SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SELECT query help


SELECT query help

Author
Message
Guras
Guras
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1363 Visits: 1232
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.
Dana
Dana
SSCertifiable
SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

Group: General Forum Members
Points: 5922 Visits: 3453
1) what have you tried?
2) do you just have the five columns?
a4apple
a4apple
Mr or Mrs. 500
Mr or Mrs. 500 (510 reputation)Mr or Mrs. 500 (510 reputation)Mr or Mrs. 500 (510 reputation)Mr or Mrs. 500 (510 reputation)Mr or Mrs. 500 (510 reputation)Mr or Mrs. 500 (510 reputation)Mr or Mrs. 500 (510 reputation)Mr or Mrs. 500 (510 reputation)

Group: General Forum Members
Points: 510 Visits: 406
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 Smile .. Visit www.sqlsaga.com for more t-sql code snippets and BI related how to articles.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search