Help with SQL Query

mrmmickle1

Well-known Member
Joined
May 11, 2012
Messages
2,461
I am trying to turn this dataset:


Excel 2012
ABCDEF
1OrderNoIDQtyUOMUnitPriceTotal
2MRM-100002Loc0027791EA72.2572.25
3MRM-100002Loc0027791EA3.093.09
4MRM-100002Loc0027791EA8.138.13
5MRM-100003Loc002717800EA0.13104
6MRM-100003Loc00271730PK2.369
7MRM-100003Loc00271730PK6.84205.2
8MRM-100003Loc00271710EA8.1381.3
9MRM-100003Loc0027172EA10.6821.36
10MRM-100005Loc002790200EA0.120
11MRM-100005Loc002790150EA0.4364.5
12MRM-100006Loc002779100EA2.08208
13MRM-100006Loc002779100EA0.4343
14MRM-100006Loc00277920EA13.07261.4
Sheet1



Into this:


Excel 2012
HIJ
1IDOrderCountTotalCost
2Loc0027792595.87
3Loc0027171480.86
4Loc002790184.5
Sheet1


By Using an SQL Query. I am unfamiliar with this calculation type query. Any help would be much appreciated.

TotalCost is calculated by summing up the Qty X UnitPrice for each Line Grouped by location.

OrderCount is calculated by counting distinct OrderNo for each ID
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I would do this in a two step process (two queries).

The first query would group by ID and OrderNo and Sum the Total amounts.
The SQL code for that query would look like this:
Code:
SELECT DataTable.ID, DataTable.OrderNo, Sum(DataTable.Total) AS SumOfTotal
FROM DataTable
GROUP BY DataTable.ID, DataTable.OrderNo;
Let's call this query "SubTotalQuery".

Now, we can use this query in another Aggregate Query to get what you want. On this one, we Group By ID, count the OrderNo field and sum the Total field.
That SQL code would look something like this:
Code:
SELECT SubTotalQuery.ID, Count(SubTotalQuery.OrderNo) AS OrderCount, Sum(SubTotalQuery.SumOfTotal) AS TotalCost
FROM SubTotalQuery
GROUP BY SubTotalQuery.ID;
and this returns what you want (shown in your original post).
 
Upvote 0
Joe4,

You nailed it!! It was a little more complex than I made out but, your guidance surely got me to the end result:

Code:
[COLOR=#0000ff]SELECT[/COLOR] SUBTOTAL.ID, Count(SUBTOTAL.OrderNo) [COLOR=#0000ff]AS[/COLOR] OrderCount, Sum(SUBTOTAL.SumOfTotal) [COLOR=#0000ff]AS[/COLOR] ProductCost
[COLOR=#0000ff]FROM [/COLOR]([COLOR=#0000ff]SELECT [/COLOR]INNERQUERY.ID , INNERQUERY.OrderNo, Sum(INNERQUERY.Total) [COLOR=#0000ff]AS[/COLOR] SumOfTotal
[COLOR=#0000ff]FROM[/COLOR]
([COLOR=#0000ff]SELECT[/COLOR] dbo_OrderNoLine.OrderNo,  dbo_OrderNo.ID, dbo_OrderNoLine.Qty, dbo_OrderNoLine.UOM, dbo_OrderNoLine.UnitPrice, dbo_OrderNoLine.UnitPrice*dbo_OrderNoLine.Qty [COLOR=#0000ff]AS [/COLOR]Total
[COLOR=#0000ff]FROM[/COLOR] dbo_OrderNo[COLOR=#0000ff] INNER JOIN[/COLOR] dbo_OrderNoLine [COLOR=#0000ff]ON[/COLOR] dbo_OrderNo.[OrderNo] = dbo_OrderNoLine.[OrderNo]
[COLOR=#0000ff]WHERE[/COLOR] (((dbo_OrderNo.Status)="Completed" [COLOR=#0000ff]AND[/COLOR] (dbo_OrderNo.ID) <> "" [COLOR=#0000ff]AND [/COLOR](dbo_OrderNo.ID) <> "Loc000999" ))) [COLOR=#0000ff]AS [/COLOR]INNERQUERY
[COLOR=#0000ff]GROUP BY[/COLOR] INNERQUERY.ID, INNERQUERY.OrderNo)  AS SUBTOTAL
[COLOR=#0000ff]GROUP BY[/COLOR] SUBTOTAL.ID;
 
Upvote 0
Great! Glad to hear it!

I was going to tell you that you could do it all in one query if you nest them, but I see that you already figured that part out.
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top