Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Using PIVOT or any other concept to transform row values to column values Rate Topic Display Mode Topic Options
Author
 Message
 Posted Wednesday, August 29, 2012 12:04 AM
 SSC 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.Result1col1 col2 col3 col4-------------------------1 X M abc2 X M pqrResult2:col1 col2 col3 col4 col5 col6--------------------------------------X M null null pqr nullX M abc null null nullFrom any one of the above results i have--and need to get result likeDesired result:col1 col2 col3 col4 col5 col6--------------------------------------X M abc null pqr nullPlease can anyone suggest any idea how to get desired result using PIVOT or any other??Remember the abc,pqr valued columns are varchar stringsThanks,Hemanth.
Post #1351404
 Posted Wednesday, August 29, 2012 12:56 AM
 Hall of Fame Group: General Forum Members Last Login: Yesterday @ 6:03 PM Points: 3,590, Visits: 5,098
 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 Group: General Forum Members Last Login: Wednesday, March 19, 2014 9:43 AM Points: 316, Visits: 1,022
 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 Group: General Forum Members Last Login: Wednesday, March 19, 2014 9:43 AM Points: 316, Visits: 1,022
 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=nullfrom #tempgroup by col2, col3;`
Post #1351504

 Permissions