Hi,
I am trying to get the max value in a range in a way that requires a double
aggregation.
For example, using Northwind,
from the resultset of this query:
select ProductName, OrderID, SUM(Quantity) from OrderDetails
group by ProductName, OrderID
order by ProductName, sum(quantity) Desc
I want to get the OrderID and quantity that ordered the most for a
ProductName
such that I get
Alice Mutton 10607 100
Aniseed Syrup 10540 60
Boston Crab Meat 10895 91
....
Thanks in advance
Habib
|
|
0
|
|
|
|
Reply
|
HSalim
|
11/19/2009 3:13:46 PM |
|
Here is one method:
SELECT ProductID, OrderID, qty
FROM (
SELECT ProductID, OrderID, SUM(Quantity) AS qty,
ROW_NUMBER() OVER(PARTITION BY ProductID ORDER BY SUM(Quantity) DESC) AS rk
FROM [Order Details]
GROUP BY ProductId, OrderID) AS P
WHERE rk = 1
ORDER BY qty DESC;
--
Plamen Ratchev
http://www.SQLStudio.com
|
|
0
|
|
|
|
Reply
|
Plamen
|
11/19/2009 4:57:38 PM
|
|
Thank you Plamen!
"Plamen Ratchev" <Plamen@SQLStudio.com> wrote in message
news:loidnevJHPth5ZjWnZ2dnUVZ_vli4p2d@speakeasy.net...
> Here is one method:
>
> SELECT ProductID, OrderID, qty
> FROM (
> SELECT ProductID, OrderID, SUM(Quantity) AS qty,
> ROW_NUMBER() OVER(PARTITION BY ProductID ORDER BY SUM(Quantity)
> DESC) AS rk
> FROM [Order Details]
> GROUP BY ProductId, OrderID) AS P
> WHERE rk = 1
> ORDER BY qty DESC;
>
> --
> Plamen Ratchev
> http://www.SQLStudio.com
|
|
0
|
|
|
|
Reply
|
HSalim
|
11/19/2009 5:44:41 PM
|
|
>> I want to get the order_id and quantity that ordered the most for a product_name <<
WITH P(product_name, order_id, qty_tot)
AS
(SELECT product_name, order_id, SUM(order_qty)
FROM Order_Details
GROUP BY product_name, order_id)
SELECT P1.*
FROM P AS P1
WHERE P1.qty_tot
= (SELECT MAX (P2.qty_tot)
FROM P AS P2
WHERE P1.product_name = P2.product_name);
This will handle ties. Get the totals in a CTE, then use it to find
the maximum total quantity.
|
|
0
|
|
|
|
Reply
|
CELKO
|
11/22/2009 8:54:28 PM
|
|
|
3 Replies
143 Views
(page loaded in 0.372 seconds)
Similiar Articles: need a query to get the values corresponding to latest dates ...One query groups by VariableName's and uses Max(Date) aggregate function to get the ... stated, I have a table structured like: > > VariableName (string) Value (double ... Jet SQL functions - microsoft.public.access.queries... and CONVERT, you probably use something like CDbl(x ) to convert x to a double ... on the Internet reveals Jet SQL functions but these are the > usual Group Aggregate ... NZ Function with Time/Date Field - microsoft.public.access ...If so, then subtracting them will still give you a double precision value that ... that does not ... does not include the specified expression as part of aggregate ... Pivot Table Values as a % of a Subtotal - microsoft.public.excel ...Programming Pivot Tables for Access Forms I also manually computed aggregates so that the pivot table's body reports the sum of the Subtotal and Freight column values as ... Sum value from Dlookup function - microsoft.public.access.forms ...Access Aggregate Functions - Access Programmers: Microsoft Access ... Access Aggregate Function Dlookup DMin DMax DAvg DCount Dfirst Dlast. Access ... Function doesn't work - microsoft.public.excel.worksheet ...----- Function IntrptFn2(sRel, tauLeft, tauRight) As Double ' =IntrptFn2($B$56,$K11,$L11 ... Open attached report. It uses the sum aggregate function and runs successfully. summarizing and finding the max value within a row over 52 column ...Dim i As Integer, vMax As Variant, tfFound As Boolean, dblCompare As Double ... you use that query as the source for a standard > query to calculate the aggregate ... hours, minutes, seconds - microsoft.public.access.gettingstarted ...Moreover it is then possible to torture it into confessing aggregate values from a ... module in your database: Public Function TimeElapsed(dblTotalTime As Double ... Cannot sum in report footer based on group footer - microsoft ...I double checked the spelling and it's correct. I even tried to use Expression ... Summing in reports - Access - Office.com Access determines how to aggregate the sum ... Formula in Query to produce Balance field - microsoft.public ...... query that doesn't include the specified expression 'date' as a part of aggregate ... Creating Calculated Fields in Access Queries and Reports finally double-clicking on ... Hallmark Equine Insurance AgencyFor example, if you have a $1 million per occurrence limit and select the double aggregate, the policy ... Wants to double aggregate extraction | Local | News | Pembroke ...H&H Aggregates is asking the Ministry of Natural Resources to increase the annual tonnage it extracts from a local quarry from 100,000 to 250,000, but first it wants ... MySQL :: Double Aggregate SUM And COUNTDouble Aggregate SUM And COUNT I am trying to calculate the total amount to charge per customer. I have a customers table and an orders table and a devices table. Double Eagle Aggregates CO Incorporated in Seymour | Double Eagle ...Find Double Eagle Aggregates CO Incorporated in Seymour with Address, Phone number from Yahoo! US Local. Includes Double Eagle Aggregates CO Incorporated Reviews ... What Does Insurance Per Project Aggregate Mean? | eHow.comA per project aggregate limit extends the limit to apply separately to each insured ... the general aggregate will either be the same as the per occurrence limit or double ... 7/27/2012 7:14:04 PM
|