We help IT Professionals succeed at work.

Query to Combine detail with sum

johnnyg123
johnnyg123 asked
on
here is some example data from my order detail table    (ms sql server 2008)

orderid   order month    Order Year       Item             Order Amount
1               Jan                           2015            Shoes           23.50
2               Jan                           2015            Gloves          34.25
3               Feb                          2015            Socks            15.33
4               Feb                          2015            Shirts             17.90

Trying to write a query that will list the detail level plus the order amount for the month and year



Using the above  (jan 2015 total is 57.75.....feb 2015 total is 33.23)

I would like following results

orderid   order month    Order Year       Item             Order Amount   MonthOrderTotal
1               Jan                           2015            Shoes           23.50                   57.75
2               Jan                           2015            Gloves          34.25                   57.75
3               Feb                          2015            Socks            15.33                   33.23    
4               Feb                          2015            Shirts             17.90                  33.23
Comment
Watch Question

SQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015
Commented:
Create a subquery to get the monthly total, then join on the detail table via month and year...
SELECT od.orderid, od.ordermonth, od.orderyear, od.item, od.amount, odmonth.MonthOrderTotal 
FROM orderdetail od 
   JOIN (
      SELECT OrderMonth, OrderYear, SUM(OrderAmount) as MonthOrderTotal
      FROM orderdetail 
      GROUP BY OrderMonth, OrderYear) odmonth ON od.ordermonth = odmonth.ordermonth AND od.orderyear = odmonth.orderyear
ORDER BY orderid

Open in new window


btw I have an image and code-heavy demo out there called SQL Server GROUP BY Solutions where point 5 illustrates this.
PadawanDBAOperational DBA
Commented:
I personally like window functions, so here ya' go:

select
	*,
	sum( orderAmount ) over( partition by orderYear, orderMonth ) as MonthlyOrderTotal
from
	OrderDetail
order by
	orderid;

Open in new window

Author

Commented:
Hi Jim

Thanks so much for the response!

Exactly what I was looking for

The Link to the demo was awesome as well...that will definitely come in handy for future reference

Wish I could award more than 500 points!
Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015

Commented:
That's fine.  PadawanDBA's answer works too, and is less code, assuming you're on 2008 or greater.  

Since I wrote an article that didn't use that windowing function, I have a hammer, everything looks like a nail, you get the idea.

Author

Commented:
I realize that since I using sql server 2008

PadawanDBA's will work

I do like Jim's approach better because we do have some servers that are not 2008 so....

Also, wanted to give extra credit for the link