Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SELECT query help Expand / Collapse
Author
Message
Posted Monday, May 5, 2014 8:43 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, December 9, 2014 2:56 PM
Points: 212, Visits: 752
I have the following data in a table

col1 col2 col3 col4 col5
1 122 AAA null null
2 122 null BBB null
3 122 null null CCC
4 156 null BBB null
5 156 AAA null null
6 156 null null CCC


I am trying to get the follwing result

122 AAA BBB CCC
156 AAA BBB CCC

( The not null value for each unique col2)

Thank you.

Post #1567543
Posted Monday, May 5, 2014 8:45 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 10:09 AM
Points: 363, Visits: 1,091
1) what have you tried?
2) do you just have the five columns?
Post #1567545
Posted Monday, May 5, 2014 8:50 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, October 8, 2014 5:53 PM
Points: 143, Visits: 316
something like this??
DECLARE @Input TABLE
(
col1 INT,
col2 INT,
col3 VARCHAR(3),
col4 VARCHAR(3),
col5 VARCHAR(3)
)

INSERT INTO @Input VALUES(1, 122, 'AAA', null, null),(2, 122, null, 'BBB', null),(3, 122, null, null, 'CCC'),
(4, 156, null, 'BBB', null), (5, 156, 'AAA', null, null), (6, 156, null, null, 'CCC')

SELECT col2, MAX(col3) as col3, MAX(col4) as col4, MAX(col5) as col5
FROM @Input
GROUP BY col2



Good Luck :) .. Visit www.sqlsaga.com for more t-sql code snippets and BI related how to articles.
Post #1567548
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse