Solved

Sql row to column

Posted on 2008-10-13
5
1,536 Views
Last Modified: 2012-06-21
I have two columns as shown below

ColA       ColB
Cash      100
Check     200
Card       300

I want result as

Cash    Check  Card
100      200      300


Here colA has run time values so i can't make it hard code



0
Comment
Question by:RETAILREALM
5 Comments
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 166 total points
ID: 22702273
DECLARE @sql varchar(MAX)

SELECT @sql = @sql + ',(SELECT SUM(ColB) FROM SomeTable WHERE ColA = ''' + ColA + ''') AS [' + ColA + ']'
FROM SomeTable
GROUP BY ColA
ORDER BY ColA

SET @sql = 'SELECT ' + SUBSTRING(@sql, 2, LEN(@sql))

EXEC(@sql)
0
 
LVL 9

Assisted Solution

by:Sander Stad
Sander Stad earned 166 total points
ID: 22702301
With the little information you gave I can only say that you could probably use the PIVOT operator to get the values the way you want.
Here are some websites that can help you:
http://www.dotnetspider.com/resources/20286-PIVOT-Operator-SQL-Server.aspx
http://articles.techrepublic.com.com/5100-10878_11-6143761.html


0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 168 total points
ID: 22702491
I think the links got crossed above, but PIVOT is good to use as suggested if you list is short and consistent as shown above as you will have to hardcode the column names or combine approaches with what Matthew showed for creating dynamic SQL.


SELECT [Cash],[Check],[Card]
FROM TableName
PIVOT (SUM(ColB) FOR ColA IN ([Cash],[Check],[Card])) pvt

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22702519
Note: PIVOT works for SQL 2005 and above; Matthew's approach works on SQL 2000 as well.  The only thing I would change is one first occurrence you want to check if NULL.
SELECT @sql = IsNull(@sql + ',', '') + '(SELECT SUM(ColB) FROM SomeTable WHERE ColA = ''' + ColA + ''') AS [' + ColA + ']'
FROM SomeTable
GROUP BY ColA
ORDER BY ColA

Open in new window

0
 

Author Closing Comment

by:RETAILREALM
ID: 31407104
Thanks guys for your help
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question