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


Need help with this query


Need help with this query

Author
Message
shahgols
shahgols
SSC Eights!
SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)

Group: General Forum Members
Points: 977 Visits: 5738
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



Ray M
Ray M
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2253 Visits: 1076
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/
shahgols
shahgols
SSC Eights!
SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)

Group: General Forum Members
Points: 977 Visits: 5738
That's exactly what I am trying right now! Smile

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.



shahgols
shahgols
SSC Eights!
SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)

Group: General Forum Members
Points: 977 Visits: 5738
Forgot, I have also tried PIVOT.



shahgols
shahgols
SSC Eights!
SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)

Group: General Forum Members
Points: 977 Visits: 5738
I figured it out, just like you said, I used MAX around each CASE statement and it ran like a charm. Thanks!



Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89945 Visits: 41146
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
shahgols
shahgols
SSC Eights!
SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)

Group: General Forum Members
Points: 977 Visits: 5738
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



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