Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sql row to column

Posted on 2008-10-13
5
Medium Priority
?
1,564 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 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 664 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 664 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 672 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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

772 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