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

Need Help in Converting Rows to Columns Expand / Collapse
Author
Message
Posted Wednesday, February 13, 2013 1:39 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, May 8, 2014 10:30 PM
Points: 32, 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.
Post #1419336
Posted Wednesday, February 13, 2013 5:09 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 8:57 AM
Points: 7,081, Visits: 12,575
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
Post #1419418
Posted Wednesday, February 13, 2013 5:10 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, July 4, 2014 3:55 AM
Points: 2,836, Visits: 5,062
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1419419
Posted Wednesday, February 13, 2013 8:50 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, July 24, 2014 2:26 PM
Points: 2,422, Visits: 7,437
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



Not a DBA, just 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


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1419588
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse