﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Need help with this query / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 18 May 2013 15:47:20 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Need help with this query</title><link>http://www.sqlservercentral.com/Forums/Topic1383815-392-1.aspx</link><description>GOOD idea, sure, here is the code:[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_3FROM   table1GROUP  BY col1,          col2 [/code]And in case you are wondering, formatting of the above SQL was done at this page (with whom I have no affiliation with):  [url]http://www.dpriver.com/pp/sqlformat.htm[/url]</description><pubDate>Tue, 13 Nov 2012 10:50:13 GMT</pubDate><dc:creator>shahgols</dc:creator></item><item><title>RE: Need help with this query</title><link>http://www.sqlservercentral.com/Forums/Topic1383815-392-1.aspx</link><description>[quote][b]shahgols (11/12/2012)[/b][hr]I figured it out, just like you said, I used MAX around each CASE statement and it ran like a charm.  Thanks![/quote]Outstanding!  Would you post your solution so that others may learn from it?  Thanks!</description><pubDate>Mon, 12 Nov 2012 17:03:43 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Need help with this query</title><link>http://www.sqlservercentral.com/Forums/Topic1383815-392-1.aspx</link><description>I figured it out, just like you said, I used MAX around each CASE statement and it ran like a charm.  Thanks!</description><pubDate>Mon, 12 Nov 2012 14:07:07 GMT</pubDate><dc:creator>shahgols</dc:creator></item><item><title>RE: Need help with this query</title><link>http://www.sqlservercentral.com/Forums/Topic1383815-392-1.aspx</link><description>Forgot, I have also tried PIVOT.</description><pubDate>Mon, 12 Nov 2012 13:07:39 GMT</pubDate><dc:creator>shahgols</dc:creator></item><item><title>RE: Need help with this query</title><link>http://www.sqlservercentral.com/Forums/Topic1383815-392-1.aspx</link><description>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_3FROM   table1GROUP  BY col1,          col2  However, when I run this I get the following errors:Msg 8120, Level 16, State 1, Line 1Column 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 1Column 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.</description><pubDate>Mon, 12 Nov 2012 13:05:13 GMT</pubDate><dc:creator>shahgols</dc:creator></item><item><title>RE: Need help with this query</title><link>http://www.sqlservercentral.com/Forums/Topic1383815-392-1.aspx</link><description>What have you tried?a couple hints.Use the case statement, wrapped in Min, or MaxGroup by Col 1, Col 2Check this out.[url=http://www.sqlservercentral.com/articles/Best+Practices/61537/]http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]</description><pubDate>Mon, 12 Nov 2012 12:54:57 GMT</pubDate><dc:creator>Ray M</dc:creator></item><item><title>Need help with this query</title><link>http://www.sqlservercentral.com/Forums/Topic1383815-392-1.aspx</link><description>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          Col41             2              A              11             2              B              21             2              C              3After:Col1          Col2           Col3_1            Col3_2         Col3_31              2                A                  B                CThe 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</description><pubDate>Mon, 12 Nov 2012 12:22:15 GMT</pubDate><dc:creator>shahgols</dc:creator></item></channel></rss>