| Author |
Topic  |
|
|
AskSQLTeam
Ask SQLTeam Question
USA
0 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
Sweden
29138 Posts |
Posted - 07/30/2007 : 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 totalshipping
from (
select
o.Customer, count(*) as ItemCount, sum(od.Amount) as OrderAmount,
o.OrderID, sum(o.ShippingCost) / count(*) as TotalShipping
from
@Orders o
inner join
@OrderDetails od on o.OrderID = od.OrderID
group by
o.Customer, o.orderid
) as d
group by customer
E 12°55'05.25" N 56°04'39.16" |
 |
|
|
spirit1
Cybernetic Yak Master
Slovenia
11741 Posts |
Posted - 07/30/2007 : 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 1980 blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
jsmith8858
Dr. Cross Join
USA
7423 Posts |
Posted - 07/30/2007 : 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 totalshipping
from (
select
o.Customer, count(*) as ItemCount, sum(od.Amount) as OrderAmount,
o.OrderID, sum(o.ShippingCost) / count(*) as TotalShipping
from
@Orders o
inner join
@OrderDetails od on o.OrderID = od.OrderID
group by
o.Customer, o.orderid
) as d
group by customer
E 1205.25" N 5639.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.
- Jeff http://weblogs.sqlteam.com/JeffS
|
Edited by - jsmith8858 on 07/30/2007 12:04:41 |
 |
|
|
sqldataguy
Starting Member
12 Posts |
Posted - 07/31/2007 : 14:10:26
|
| Great article. Looking forward to Part II |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
Sweden
29138 Posts |
|
|
bhaaskar
Starting Member
1 Posts |
Posted - 08/12/2010 : 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.0000 2 ABC 2007-01-02 30.0000 5 Item H 200.0000 3 ABC 2007-01-03 25.0000 6 Item X 100.0000 4 DEF 2007-01-02 10.0000 8 Item Z 300.0000
I am using SQL7.0. SQL2000 solution would work too.
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47157 Posts |
Posted - 08/12/2010 : 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.0000 2 ABC 2007-01-02 30.0000 5 Item H 200.0000 3 ABC 2007-01-03 25.0000 6 Item X 100.0000 4 DEF 2007-01-02 10.0000 8 Item Z 300.0000
I am using SQL7.0. SQL2000 solution would work too.
SELECT o.OrderID,od.Item,od.Amount
FROM Orders o
INNER JOIN OrderDetails od
ON od.OrderID = o.OrderID
INNER JOIN (SELECT OrderID,MAX(Amount) AS MaxAmt
FROM OrderDetails
GROUP BY OrderID)od1
ON od1.OrderID = od.OrderID
AND od1.MaxAmt = od.Amount
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
| |
Topic  |
|