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

Need help with this query Expand / Collapse
Author
Message
Posted Monday, November 12, 2012 12:22 PM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, July 28, 2014 4:29 PM
Points: 627, Visits: 4,976
Hi all,

I have to make the following, but I've had no luck getting it done. Can you please help? Thanks in advance.

Before:

Col1 Col2 Col3 Col4
1 2 A 1
1 2 B 2
1 2 C 3

After:

Col1 Col2 Col3_1 Col3_2 Col3_3
1 2 A B C

The data includes only 3 rows max for combination of Col1 and Col2. And the results have to be ordered using Col4, Ascending. Thanks again.

There can only be



Post #1383815
Posted Monday, November 12, 2012 12:54 PM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Wednesday, August 13, 2014 12:03 PM
Points: 1,480, Visits: 1,028
What have you tried?

a couple hints.

Use the case statement, wrapped in Min, or Max
Group by Col 1, Col 2

Check this out.
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1383833
Posted Monday, November 12, 2012 1:05 PM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, July 28, 2014 4:29 PM
Points: 627, Visits: 4,976
That's exactly what I am trying right now! :)

I got this so far:

SELECT col1,
col2,
CASE col4
WHEN 1 THEN col3
ELSE NULL
END AS col3_1,
CASE col4
WHEN 2 THEN col3
ELSE NULL
END AS col3_2,
CASE col4
WHEN 3 THEN col3
ELSE NULL
END AS col3_3
FROM table1
GROUP BY col1,
col2

However, when I run this I get the following errors:

Msg 8120, Level 16, State 1, Line 1
Column col3 is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 1
Column col4 is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

I'm working on it, but if you have the solution, please don't hesitate to post it. Thanks for your input by the way.



Post #1383840
Posted Monday, November 12, 2012 1:07 PM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, July 28, 2014 4:29 PM
Points: 627, Visits: 4,976
Forgot, I have also tried PIVOT.


Post #1383842
Posted Monday, November 12, 2012 2:07 PM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, July 28, 2014 4:29 PM
Points: 627, Visits: 4,976
I figured it out, just like you said, I used MAX around each CASE statement and it ran like a charm. Thanks!


Post #1383867
Posted Monday, November 12, 2012 5:03 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:17 AM
Points: 36,944, Visits: 31,448
shahgols (11/12/2012)
I figured it out, just like you said, I used MAX around each CASE statement and it ran like a charm. Thanks!


Outstanding! Would you post your solution so that others may learn from it? Thanks!


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1383913
Posted Tuesday, November 13, 2012 10:50 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, July 28, 2014 4:29 PM
Points: 627, Visits: 4,976
GOOD idea, sure, here is the code:

SELECT col1,
col2,
Max(CASE col4
WHEN 1 THEN col3
ELSE NULL
END) AS col3_1,
Max(CASE col4
WHEN 2 THEN col3
ELSE NULL
END) AS col3_2,
Max(CASE col4
WHEN 3 THEN col3
ELSE NULL
END) AS col3_3
FROM table1
GROUP BY col1,
col2


And in case you are wondering, formatting of the above SQL was done at this page (with whom I have no affiliation with):

http://www.dpriver.com/pp/sqlformat.htm



Post #1384201
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse