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
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: 2 days ago @ 12:02 AM
Points: 29, Visits: 107
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



Group: General Forum Members
Last Login: Yesterday @ 9:25 AM
Points: 7,070, Visits: 12,523
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

Believe you can and you're halfway there. --Theodore Roosevelt

Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein

The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein

1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
Post #1419418
Posted Wednesday, February 13, 2013 5:10 AM


Group: General Forum Members
Last Login: Thursday, April 03, 2014 10:10 AM
Points: 2,792, Visits: 4,874
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



Group: General Forum Members
Last Login: Today @ 7:06 AM
Points: 2,404, Visits: 7,311
IF object_id('tempdb..#testEnvironment') IS NOT NULL
DROP TABLE #testEnvironment;

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

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: -
SELECT @SQL = 'SELECT dtDate,' + sqlCode + CHAR(13) + CHAR(10) + 'FROM #testEnvironment'+CHAR(13)+CHAR(10)+'GROUP BY dtDate;'
'MAX(CASE WHEN sDocIdPrefix = ' + CAST(sDocIdPrefix AS VARCHAR(20)) + ' THEN CNT ELSE 0 END) AS '+
FROM #testEnvironment

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...

For better, quicker answers on SQL Server performance related questions, click on the following...

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