SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Merge several rows to one row
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

msallem
Starting Member

USA
2 Posts

Posted - 12/18/2012 :  15:33:40  Show Profile  Reply with Quote
In SQL 2012, I have a table: my_table that has columns: state, month, ID, and sales. my goal is to merge different rows that have the same state, month and ID into one row while summing the sales column of these selected rows into the merged row. For example:

state month ID sales
FL June 0001 12,000
FL June 0001 6,000
FL June 0001 3,000
FL July 0001 6,000
FL July 0001 4,000
TX January 0050 1,000
MI April 0032 5,000
MI April 0032 8,000
CA April 0032 2,000
This what I am supposed to get

state month ID sales
FL June 0001 21,000
FL July 0001 10,000
TX January 0050 1,000
MI April 0032 13,000
CA April 0032 2,000
I did some research, and I found that the self join is supposed to do something similar to what I am supposed to get. Any help would be greatly appreciated. Thank you!

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/18/2012 :  15:44:28  Show Profile  Reply with Quote
Just group by state,month and ID and do SUM of sales.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 12/19/2012 :  00:57:26  Show Profile  Reply with Quote
No need to join. Read about GROUP BY in BOL as Sodeep suggested

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

msallem
Starting Member

USA
2 Posts

Posted - 12/19/2012 :  13:49:11  Show Profile  Reply with Quote
Thank you!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000