I need to show the order ID and the order total for largest order (order ID-dollar amount)? - subquery

The Database. I need to find the order ID and the largest order in terms of dollars using SQL. I attached the OM database. Using SQL I think I would need to use group by or order by but I just need to find the largest order.
select order_id from orders
Union
sum(unit_price) from items;
It resulted in an error. I wanted it to show the order id and the sum of the unit_price and get the largest unit price to get the largest order. I’m getting an error message saying query without GROUP BY and nonaggregated column.

Related

NetSuite formula to show quantity ordred on first order

We have a saved search in place that displays date of first order and last order by customer & item within a given date range. For example - Looking at sales for May 2022 - today, it shows the item, the customer, the date they first ordered the item, and the date they last ordered the item.
I am now also trying to incorporate the quantity ordered on the first order.
crietera
results
I've tried the following, but keep getting an invalid expression. Can anyone advise on how I might be able to display the qty on the date of the first order?
MIN(CASE WHEN {custbody_reporting_ship_date} THEN {quantity} END)
and
CASE WHEN (MIN({custbody_reporting_ship_date})THEN {quantity} END)
I think this can't be achieved in saved search via simple formula. You may want to try below, what I will do is try to record the first and last order id in the customer record, then from customer saved search you can source from these order fields to the order details.
Create 2 custom entity fields applied to customer
first order id => Type:List/Record => List/Record:Transaction
last order id => Type:List/Record => List/Record:Transaction
Create 1 saved search to get the first order id per customer
Similar to the one that you created, the result field just need the customer internal id and min(document number) and filter need to add customer.internal id
Create 1 saved search to get the last order id per customer
Similar to the one that you created, the result field just need the customer internal id and max(document number) and filter need to add customer.internal id
Create 2 scheduled workflows, to update the first order id and last order id fields in respective customer record.
Create your first and last order customer Saved Searches using the 2 custom fields to source for the order details.
Not sure if this help.

Cassandra use aggregate function and then order by that aggregate

I have a cassandra database with a table that has the following columns:
itemid
userid
rating
itemid and userid are the primary key. My query looks like this:
SELECT itemid, avg(rating) as avgRating from mytable GROUP BY itemid order by avgRating asc;
I get the following error:
InvalidRequest: Error from server: code=2200 [Invalid query] message="ORDER BY is only supported when the partition key is restricted by an EQ or an IN."
How can I fix this?
I need to order by the average ratings after so I can get the top 10 movies based on their average rating.
Cassandra can only order results by clustering column(s). It cannot order results by an aggregate function.
There are a couple of options you could look at in order to accomplish this.
Make the query and then re-order the results in your application.
This option may work if you only expect a limited number of rows to be returned from each query.
Note that it is recommended that you only use aggregate functions (like avg()) when you know that it will only apply to a limited number of rows. Ideally you should only use them when operating on a single partition (use a WHERE clause to limit to a single partition). If you don't have any limit you may see very slow queries, or query timeouts if Cassandra needs to read a large number of rows in order to calculate the aggregate.
Store a pre-calculated average in the table, or cache it in your application.
This is the best option if you need calculated averages over a larger data set.
If you make average_rating a clustering column Cassandra will store the averages for each partition in sorted order. This is very efficient from Cassandra's perspective.
The downside is that you'll need to calculate the average in your application each time you insert into or update a row, because it will be a primary key column in your Cassandra table.
One thing you could look into is using a Cassandra trigger to calculate the average for you. This may make life easier for you if you have multiple applications writing to this table, however I am not actually sure if it is possible to modify a primary key column via a custom trigger. I would recommend doing some research & testing if you decide to look at this option. You can read about triggers here.

Group and sum columns based on values (Query)

I would like to group and sum a table based on values (just like a pivot table would do, but without it) but without using it.
Example Table:
For example, in this query I would like to group each row that contains the same RU and TP and sum the Balance Value.
I tried using the group by like this Group by:
but it does not return every RU possible, if a value is in the TP column it does not appear in RU column.
EDIT1: My table starts like this
and when I group and sort using group by in query the result is
the result is what I want, but as you can see, the 0156 and 0195 RUs, for some reason, are deleted from my table, and a lot of others RUs too.

How to retrieve item closest to another item in DynamoDB?

I have a dynamo DB table where the sort key has a numeric value.
I have a requirement to retrieve the first item which has a lower value than the one, that I have.
I have gone through http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html#API_UpdateItem_Examples docs but I can see no way to:
- sort the output
- limit the result to 1 entry
Is there any way to actually achieve what I want with dynamo DB?
EDIT:
According to this: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html
The results are sorted using sorting key, and when it's numeric, they are sorted descending. Which is great, but I still can't find any way to get only a single result [don't want to "pay" for the full table scan in some cases].
Are you searching for the next item which has a lower sort key within the same Partition Key?
In that case, you are able to use Query as you've found, sort in Descending and Limit to 1. This will not scan the entire table.
Alternatively, if you wish you scan cross Partitions, unfortunately a Table Scan is the only way to do this.

Dynamic Grouping in Power BI/PowerPivot Model based on a calculated field

We are trying to create a model in Excel/Power BI (using Power Query or Power Pivot or anything that would work) in order to classify a customer by its best product (based on a ranking system).
The first approach we applied was to count the customers per minimum ranking (or per best product brand). (inspired from the blog https://stackoverflow.com/questions/15742186/powerpivot-dax-dynamic-ranking-per-group-min-per-group)
Below the steps we did exactly:
- In PowerPivot Model, we created Classification and Customers table like in the example further below.
In the same model, we added a calculated column with the following formula to obtain the minimum rank per customer.
=MINX (
FILTER ( ALLSELECTED ( Customers ); [Customer_ID] = EARLIEST ( [Customer_ID] ) );
[Ranking]
)
Within a pivot table in Excel, we’ve put the calculated column in rows.
Then, we’ve used a Count distinct aggregation of the customers in the pivot table values.
 This gave me the first desired result. (below example Pivot_Table.Selection1)
Now, the issue comes when we want to add more analysis axis.
For example, besides the product brand, we want to have the Product type in columns, and we want our measure to be recalculated every time I add/delete an axis.
In other words, we want to have a distinct customer count per best product and per Product Type.
In addition, we want the second attribute (axis) to be variable and the grouping or the distinct count per group to be dynamic.
Example:
Let’s suppose we have the tables Classification and Customers in our Model:
In the first approach we tried, we got the following table: Pivot_Table.Selection1:
Now when we add the analysis axis, we would like to have the following example: Pivot_Table.Selection2:
But we are having this:
As you can see, there should be one customer for the Group “Mercedes” and one for “Renault”, since depending on the product type, the top Truck for customer A is Renault and its top Car is “Mercedes”.
However, in the pivot table, the Mercedes group is shown as Truck (which doesn’t even exist in our dataset).
Edit
I'm open for any suggestion, not only Power Pivot, but also Power Query (M functions) or Power BI or whatever could work.
Finally I think I understood your problem, a customer can have different Product_Brand values, you want to count only those Product_Brand which its ranking is the minimum.
In that case, this is a possible solution:
Create a calculated column called Minimum Rank in the Customer table.
=
CALCULATE (
MIN ( [Ranking] );
FILTER ( Customer; [Customer_ID] = EARLIER ( Customer[Customer_ID] ) )
)
Then create a measure, lets say Customer ID Distinct Count to count those rows where the Rank is equal to the minimum for that customer.
Customer ID Distinct Count :=
CALCULATE (
DISTINCTCOUNT ( Customer[Customer_ID] );
FILTER ( Customer; [Ranking] = [Minimum Rank] )
)
You will get something like this:

Resources