Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2007-07-30 : 10:16:43
|
Summarizing data in a SELECT statement using a GROUP BY clause is a very common area of difficulty for beginning SQL programmers. In Part I of this two part series, we'll use a simple schema and a typical report request to cover the effect of JOINS on grouping and aggregate calculations, and how to use COUNT(Distinct) to overcome this. In Part II, we'll finish up our report while examining the problem with SUM(Distinct) and discussing how useful derived tables can be when grouping complicated data. Read How to Use GROUP BY in SQL Server |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-30 : 10:38:24
|
Great article!I hope the solution is far better than using a derived table like thisselect customer, sum(itemcount) AS itemcount, sum(orderamount) as orderamount,count(distinct orderid) as ordercount, sum(totalshipping) as totalshippingfrom (select o.Customer, count(*) as ItemCount, sum(od.Amount) as OrderAmount, o.OrderID, sum(o.ShippingCost) / count(*) as TotalShippingfrom @Orders oinner join @OrderDetails od on o.OrderID = od.OrderIDgroup by o.Customer, o.orderid) as dgroup by customer E 12°55'05.25"N 56°04'39.16" |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-07-30 : 10:57:05
|
i bet 5 cents on an (sum() over ...) implementation seriously though, how can something that simple be so hard to understand???_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-07-30 : 11:41:25
|
quote: Originally posted by Peso Great article!I hope the solution is far better than using a derived table like thisselect customer, sum(itemcount) AS itemcount, sum(orderamount) as orderamount,count(distinct orderid) as ordercount, sum(totalshipping) as totalshippingfrom (select o.Customer, count(*) as ItemCount, sum(od.Amount) as OrderAmount, o.OrderID, sum(o.ShippingCost) / count(*) as TotalShippingfrom @Orders oinner join @OrderDetails od on o.OrderID = od.OrderIDgroup by o.Customer, o.orderid) as dgroup by customer E 12�05.25"N 56�39.16"
I knew someone would feel compelled try to spoil part II ... oh well. Yes, it is similar to that, but no need for any COUNT(Distinct) or dividing anything by COUNT(*) expressions, it is of course much simpler. the article is targeted towards beginners, not for sql experts. I tried to really talk about how grouping and summarizing can cause difficulty with duplicates caused by JOINS, something we see people struggle with all the time. Also, I keep noticing that some people use SUM(Distinct) which doesn't really solve their problems, so I mention that in part II as well ... I didn't use any SQL 2005 features because they were a little out of the scope and not necessary, plus I wanted to stick with the basics for beginners as best I could.- Jeffhttp://weblogs.sqlteam.com/JeffS |
|
|
sqldataguy
Starting Member
12 Posts |
Posted - 2007-07-31 : 14:10:26
|
Great article. Looking forward to Part II |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
bhaaskar
Starting Member
1 Post |
Posted - 2010-08-12 : 09:45:27
|
My first post here: if this has already been answered, kindly point me there. Thank you very much. From the tables Orders and order details tables mentioned in the article (http://www.sqlteam.com/article/how-to-use-group-by-in-sql-server) I would like to get: for each order, the item name of the max priced product in that order.The result set would be:orderID customer orderdate shippingCost DetailID Item Amount ----------- ---------- -------------- ------------- --------- ---------- ----------1 ABC 2007-01-01 40.0000 2 Item B 150.00002 ABC 2007-01-02 30.0000 5 Item H 200.00003 ABC 2007-01-03 25.0000 6 Item X 100.00004 DEF 2007-01-02 10.0000 8 Item Z 300.0000I am using SQL7.0. SQL2000 solution would work too. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-12 : 14:23:40
|
quote: Originally posted by bhaaskar My first post here: if this has already been answered, kindly point me there. Thank you very much. From the tables Orders and order details tables mentioned in the article (http://www.sqlteam.com/article/how-to-use-group-by-in-sql-server) I would like to get: for each order, the item name of the max priced product in that order.The result set would be:orderID customer orderdate shippingCost DetailID Item Amount ----------- ---------- -------------- ------------- --------- ---------- ----------1 ABC 2007-01-01 40.0000 2 Item B 150.00002 ABC 2007-01-02 30.0000 5 Item H 200.00003 ABC 2007-01-03 25.0000 6 Item X 100.00004 DEF 2007-01-02 10.0000 8 Item Z 300.0000I am using SQL7.0. SQL2000 solution would work too.
SELECT o.OrderID,od.Item,od.AmountFROM Orders oINNER JOIN OrderDetails odON od.OrderID = o.OrderIDINNER JOIN (SELECT OrderID,MAX(Amount) AS MaxAmt FROM OrderDetails GROUP BY OrderID)od1ON od1.OrderID = od.OrderIDAND od1.MaxAmt = od.Amount ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|