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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Merge rows into one?

Author  Topic 

rubo81
Starting Member

2 Posts

Posted - 2008-03-25 : 00:33:20
Does anyone have any idea how to construct a select statement querying a temp table that contains the following data:

Code | Jan-Apr | May-Aug | Sep-Dec
----------------------------------------
ABCD | 250.00 | NULL | NULL
ABCD | NULL | 889.56 | NULL
ABCD | NULL | NULL | 1233.67

... that will merge the results as follows?

Code | Jan-Apr | May-Aug | Sep-Dec
----------------------------------------
ABCD | 250.00 | 889.56 | 1233.67


Your help would be greatly appreciated!

Thanks.

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2008-03-25 : 00:47:14
try this,

SELECT Code, MAX(Jan-Apr) as 'Jan-Apr', ...
FROM tbl
GROUP BY Code
Go to Top of Page

rubo81
Starting Member

2 Posts

Posted - 2008-03-25 : 00:53:20
Thanks SOOOOOOO much, Peter!! I knew there had to be a simple way, I was just not seeing it!
Go to Top of Page

mahesh_bote
Constraint Violating Yak Guru

298 Posts

Posted - 2008-03-25 : 02:03:18
Select Code, SUM(Jan-Apr) As Jan-Apr, SUM(May-Aug) As May-Aug,
SUM(Sep-Dec) As Sep-Dec From <Table> Group By Code

Thanks,

Mahesh
Go to Top of Page
   

- Advertisement -