## 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
Solved

# Simple Pivot Question

Posted on 2012-03-21
340 Views
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
Question by:chrisli

LVL 15

Expert Comment

ID: 37749700
``````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
``````
0

Author Comment

ID: 37750240
Thanks, but what if the # of acctno is not dynamic(more than 3)?
0

LVL 39

Accepted Solution

appari earned 500 total points
ID: 37750746
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 ')
``````
0

LVL 20

Expert Comment

ID: 37751309
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

ID: 37754301
Thanks works great
0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even withinâ€¦
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includâ€¦
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.