how to find an average of attributes in Xquery? - attributes

This is given:
<tourism>
<hotel name = "radisson">
<room id="001" price = "150">
<room id="002" price = "250">
</hotel>
</tourism>
I am trying to find the average of the prices for every hotel inside tourism. Is this the right code?
The output should be name of hotel and average price of rooms in that hotel.
FOR $h in (document("tourism.xml")tourism/hotel[name=$n]
LET $a = avg(document("tourism.xml") $h/#price)
RETURN <tourism>
$n //returns hotel name
$a //returns average prices of all rooms
</tourism>

How about something like this?
<tourism>
{
for $hotel in doc("tourism.xml")/tourism/hotel
return
<hotel>
<name>{string($hotel/#name)}</name>
<average>{avg($hotel/room/xs:float(#price)}</average>
</hotel>
}
</tourism>

Related

How to get price of actual product in Drupal Commerce?

Hi guys im new in drupal, i start to learn drupal 3 month before.
Im trying to get the price number of the product i get the total cart like this
$user_id = \Drupal::currentUser()->id();
$orders = \Drupal::entityTypeManager()
->getStorage('commerce_order')
->loadByProperties(['uid' => $user_id]);
$orderPrice = [];
foreach($orders as $key => $value) {
$orderPrice[] = $value->getTotalPrice()->getNumber();
}
$total = $orderPrice[count($orderPrice)-1];
$total= number_format($total, 2);
Im trying to do something similar with te product.
For example if iam in the product 1 get the price of product 1, if i am in the product 2 get the price of product 2.
Thanks for your time :)

How to get Remaining Amount of Customer Deposits in Saved Search (includes SuiteScript code)

I need to get the Remaining Balance of all Customer Deposits linked to a specific Sales Order
Remaining Balance = the available/unapplied amount
Below, I will show you how I solved this in a saved search.
As a bonus, I'll also include the SuiteScript 1.0 code to do the same.
To show the Remaining Amount of all Customer Deposits linked to a specific Sales Order record:
Create a Transaction Saved Search as follows:
Criteria (Use Expressions)
( Type IS Deposit Application AND
Created From Fields > Sales Order IS Sales Order #xyz ) OR
( Type IS Customer Deposit AND
Created From IS Sales Order #xyz )
Results
Formula (Numeric) (Summary type SUM): CASE WHEN {type} = 'Customer Deposit' THEN {debitamount} ELSE -{creditamount} END
BONUS, SuiteScript 1.0 code to get Remaining Amount of all Customer Deposits linked to a specific Sales Order record:
function customerDepositsRemainingBalance(salesorder_internalid) {
var filters = [[["type","anyof","DepAppl"],"AND",["createdfrom.salesorder","anyof",salesorder_internalid]],"OR",[["type","anyof","CustDep"],"AND",["createdfrom","anyof",salesorder_internalid]]];
var columns = [new nlobjSearchColumn('formulanumeric',null,'SUM')];
columns[0].setFormula("CASE WHEN {type} = 'Customer Deposit' THEN {debitamount} ELSE -{creditamount} END");
var search = nlapiSearchRecord('transaction',null,filters,columns);
if(search == null) return 0;
return Number(search[0].getValue(columns[0]));
}
// Example Usage
balance = customerDepositsRemainingBalance(3247434); // returns 50
balance = customerDepositsRemainingBalance(3256644); // returns 0

Request for all entries in a Contentful space not finding entries by field name

I have a content model: Category, with two entries for the Category content.
which I am retrieving with this function:
def categories(request):
contentful_client = contentful.Client(CONTENTFUL_SPACE_ID,
CONTENTFUL_TOKEN_ID)
category_entries = contentful_client.entries()
for entry in category_entries:
print(getattr(entry, 'categoryName', 'not found'))
for entry in category_entries:
print(entry)
count = len(category_entries)
return HttpResponse('There are %d categories' % count)
The output for this is:
not found
not found
<Entry[category] id='7hzRlNZw9cvKeYBQPGRV9a'>
<Entry[category] id='2ZV3W30qLSosL5PPmKEoD5'>
I'm wondering why the attribute categoryName not being recognized since the entry has two attributes categoryName and categoryBlob
The documentation I'm following does it in the same way:
for entry in entries:
print(getattr(entry, 'product_name', 'Not a product'))
Not a product
Not a product
Whisk Beater
Hudson Wall Cup
Not a product
Not a product
Not a product
Not a product
SoSo Wall Clock
Not a product
Not a product
Not a product
Playsam Streamliner Classic Car, Espresso
What am I missing?
Apparently, getattr doesn't allow camelcase arguments.
I changed:
print(getattr(entry, 'categoryName', 'not found'))
to:
print(getattr(entry, 'category_name', 'not found'))
even though the name of the attribute is categoryName and it worked.

Filter Result Best match result at top and then follow remaining results

I am developing one city filter in mongodb and nodejs with list of city with checkbox like this
city name 1
city name 2
city name 3
city name 4
once we click on any city or multiple city then we receive data with related to that city with following code.
var query = {city:{ $in:city }};
posts.find(query, {} , function(e,docs){
res.json(docs);
});
through query i just pass the city as array and receive Data..But my requirement is :
1.Suppose if i selected any 1 or 2 city then that city result come first and remaining city follow after that city even if those city not selected.
Thanks.
Query1 : db.city.find({name:{$in:["Bangalore"]}})
Query2 - db.city.find({name:{$nin:["Bangalore"]}})
**
- Edit
**
> var result = []; //Define an empty Array
> var cur = db.city.aggregate([{$match:{name:{$in:["Bangalore","Delhi"]}}}]); //Get matching city and save it in cursor
> while(cur.hasNext()){ result.push(cur.next()) } // Retrieve the cursor data and push it in array
> var cur = db.city.aggregate([{$match:{name:{$nin:["Bangalore","Delhi"]}}}]); // Get non matching city and save it in cursor (should be new variable)
> while(cur.hasNext()){ result.push(cur.next()) }
> for(var i=0;i<result.length;i++){printjson(result[i])}
This will give you expected result. Hope you are expecting the same :)

Displaying the total sum of values from strings

My project is a simple shopping game where the user types in the quantity amount, and the value of the individual prices appears and then a total sum can appear below.
I have managed to create the part of displaying the individual product price but i am confused on how to add the total sum and display correctly at the instance that i defined.
Some info
Actionscript will check for keypress event
sample of code snippet:
if(e.keyCode == 49){ //1
trace("Key Code Pressed: " + e.keyCode);
amount1.text = "1.00"
}
...
var total:Number = amount1+ amount2+amount3+amount4+amount5;
output1.text = String(total);
From the code above, when the user types 1, the price will change to "1.00" on the price instance field (dynamic text type).
Picture below:
A sample of my game running:
Total price should be $13.00 dollars..
Is there any way to make this happen? I believe is it something to do with parseint.
You should be able to string together multiple parseint statements like this:
var total:number = parseint(amount1.text) + parseint(amount2.text) + parseint(amount3.text) + parseint(amount4.text) + parseint(amount5.text);
output1.text = total;
If you go this route, you will need to handle stituations that involve NaN
Here is the documentation on parseint if you haven't already, you should look at it. http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000590.html

Resources