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

Using PIVOT or any other concept to transform row values to column values Expand / Collapse
Author
Message
Posted Wednesday, August 29, 2012 12:04 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, August 30, 2012 7:58 AM
Points: 35, Visits: 122
Hi Everyone,

I am in need of display results but stuck in middle.

I have two results different ways.

Result1

col1 col2 col3 col4
-------------------------
1 X M abc
2 X M pqr



Result2:
col1 col2 col3 col4 col5 col6
--------------------------------------
X M null null pqr null
X M abc null null null


From any one of the above results i have--and need to get result like

Desired result:

col1 col2 col3 col4 col5 col6
--------------------------------------
X M abc null pqr null


Please can anyone suggest any idea how to get desired result using PIVOT or any other??

Remember the abc,pqr valued columns are varchar strings


Thanks,
Hemanth.






Post #1351404
Posted Wednesday, August 29, 2012 12:56 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:12 PM
Points: 3,609, Visits: 5,220
How do you know that abc belongs in col3 and pqr belongs in col5?


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1351423
Posted Wednesday, August 29, 2012 3:08 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, June 27, 2014 8:03 AM
Points: 320, Visits: 1,079
This looks easy enough to solve given a bit more information.

Can you post the queries that gave the results above, and if you posted some sample data you would most likely get a solution straight away.
Post #1351484
Posted Wednesday, August 29, 2012 4:21 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, June 27, 2014 8:03 AM
Points: 320, Visits: 1,079
Based on what we've got so far, this could be what you want:


--=======================================================
-- Test data:
--=======================================================
drop table #temp;

create table #temp
(
col1 int,
col2 char(1),
col3 char(1),
col4 char(3)
);

insert #temp values ( 1, 'X', 'M', 'abc' );
insert #temp values ( 2, 'X', 'M', 'pqr' );

--=======================================================
-- Solution:
--=======================================================
select
col1=col2,
col2=col3,
col3=max(case when col1=1 then col4 else null end),
col4=null,
col5=max(case when col1=2 then col4 else null end),
col6=null
from #temp
group by col2, col3;



Post #1351504
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse