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


Need Help in Converting Rows to Columns


Need Help in Converting Rows to Columns

Author
Message
jeganbenitto.francis
jeganbenitto.francis
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 113
Hi All,
Can anyone help me in below problem.
I have below result set
dtDate sDocIdPrefix CNT
2/11/2013 653 3683
2/11/2013 649 1334
2/12/2013 647 1422
2/12/2013 649 6602
2/12/2013 653 4781

And i want it to be displayed as below

dtDate 653 649 647
2/11/2013 3683 1334 0
2/12/2013 6602 6602 1422


Can Anyone please help on this . URGENT!!!!

Thanks in Advance.
Orlando Colamatteo
Orlando Colamatteo
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38278 Visits: 14411
If you provide test tables in the form of CREATE TABLE statements and sample data in the form of INSERT INTO statements I would be happy to provide some working code to help you solve the issue.

Without that I can refer you to this article:

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns By Jeff Moden]


edit: fix url

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12042 Visits: 5478
If it's really URGENT, then you better to present your issue in a way it could be quickly addressed. Please have a read tips from the link at the bottom of my signature.
DDL, sample data insert script and clear example of output (which you have already) will be very helpful.
Otherwise you need to wait for someone less lazy to do it for you. I can assure you that it will be definitely done, but it will take a bit longer than "URGENT" :-)

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Cadavre
Cadavre
SSCrazy Eights
SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)

Group: General Forum Members
Points: 8768 Visits: 8490
IF object_id('tempdb..#testEnvironment') IS NOT NULL
BEGIN
DROP TABLE #testEnvironment;
END;

SELECT CAST(dtDate AS DATETIME) AS dtDate, sDocIdPrefix, CNT
INTO #testEnvironment
FROM (SELECT '2013-02-11', 653, 3683 UNION ALL
SELECT '2013-02-11', 649, 1334 UNION ALL
SELECT '2013-02-12', 647, 1422 UNION ALL
SELECT '2013-02-12', 649, 6602 UNION ALL
SELECT '2013-02-12', 653, 4781
)a(dtDate,sDocIdPrefix,CNT);



Sample data is: -
dtDate                  sDocIdPrefix CNT
----------------------- ------------ -----------
2013-02-11 00:00:00.000 653 3683
2013-02-11 00:00:00.000 649 1334
2013-02-12 00:00:00.000 647 1422
2013-02-12 00:00:00.000 649 6602
2013-02-12 00:00:00.000 653 4781


Using the sample data above, then I think that this is what you're after: -
DECLARE @SQL NVARCHAR(MAX);
SELECT @SQL = 'SELECT dtDate,' + sqlCode + CHAR(13) + CHAR(10) + 'FROM #testEnvironment'+CHAR(13)+CHAR(10)+'GROUP BY dtDate;'
FROM (SELECT STUFF((SELECT ',' + CHAR(13)+CHAR(10)+
'MAX(CASE WHEN sDocIdPrefix = ' + CAST(sDocIdPrefix AS VARCHAR(20)) + ' THEN CNT ELSE 0 END) AS '+
QUOTENAME(CAST(sDocIdPrefix AS VARCHAR(20)))
FROM (SELECT DISTINCT sDocIdPrefix
FROM #testEnvironment
)a
ORDER BY sDocIdPrefix DESC
FOR XML PATH(''),TYPE
).value('.','VARCHAR(MAX)'),1,1,'')
)dynSql(sqlCode)

EXECUTE sp_executesql @SQL;



Produces: -
dtDate                  653         649         647
----------------------- ----------- ----------- -----------
2013-02-11 00:00:00.000 3683 1334 0
2013-02-12 00:00:00.000 4781 6602 1422


Look at how I laid out the sample data, it makes it considerably easier for people to help when it looks like that


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
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