Query to Combine detail with sum

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
johnnyg123Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PadawanDBAOperational DBACommented:
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

0
johnnyg123Author 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!
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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.
0
johnnyg123Author 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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.