Solved

Simple Pivot Question

Posted on 2012-03-21
5
309 Views
Last Modified: 2012-03-22
have a query (select  [AcctNo], [shares], [symbol] from MyTable) that returns:

[AcctNo], [shares], [symbol]
acct01,    100 ,  msft
acct02,    150,  msft
acct02,    200,  aapl
acct02,    250 ,  aapl
acct03,    300 ,  intl
.
..

I'd like to pivot the data so it displays as:
[ ] ,    [acct01] , [acct02], [acct03], .....
msft,        100,         150,      0
aapl,             0,         450,     0
intl ,              0,             0,     300
.
..
Number of [AcctNo] and [symbol] are undetermined.

Any help on the SQL here would be appreciated.
0
Comment
Question by:chrisli
5 Comments
 
LVL 15

Expert Comment

by:tim_cs
Comment Utility
DECLARE @Table TABLE (AcctNo VARCHAR(20),shares INT, symbol VARCHAR(20))

INSERT INTO @Table
        (AcctNo, shares, symbol)
VALUES
        ('acct01',100,'msft')
		,('acct02',150,'msft')
		,('acct02',200,'aapl')
		,('acct02',250,'aapl')
		,('acct03',300,'intl')

SELECT
	symbol
	,COALESCE([acct01],0) Acct01
	,COALESCE([acct02],0) Acct02
	,COALESCE([acct03],0) Acct03
FROM
	@Table
PIVOT (SUM(shares) FOR AcctNo IN ([acct01]
	,[acct02]
	,[acct03])) pvt

Open in new window

0
 

Author Comment

by:chrisli
Comment Utility
Thanks, but what if the # of acctno is not dynamic(more than 3)?
0
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
Comment Utility
try this
DECLARE @accounts VARCHAR(max)
;with accounts as (Select distinct AcctNo From MyTable)
Select @accounts = COALESCE( '[' + AcctNo + '],' + @accounts,'[' + AcctNo + ']') from accounts 

Exec('Select symbol,' + @accounts + ' From  MyTable PIVOT (Sum(shares) for AcctNo in (' + @accounts + ')) as PVT ')

Open in new window

0
 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
See this for your reference, a very good discussion on problem that you have...
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL_Server_2008/Q_27633177.html#a37724982
0
 

Author Closing Comment

by:chrisli
Comment Utility
Thanks works great
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now