Solved

Sql row to column

Posted on 2008-10-13
5
1,539 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

733 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