Solved

Sql row to column

Posted on 2008-10-13
5
1,549 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 60

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 60

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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

631 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