SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On



Rows to columns + count Expand / Collapse
Author
Message
Posted Thursday, November 19, 2009 2:05 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Tuesday, February 16, 2010 11:22 AM
Points: 549, Visits: 746
I'm on SQL Server 2000.
Here is my data:

Index	    CUSIP	DataSrc
------------------------------
A 556 MHS
A 556 MHS
A 556 MHS
A 556 FRDOps
A 556 EJV
A 557 MHS
A 557 FRDOps
A 557 EJV

How do convert it to:

Index	  CUSIP  MHS  FRDOps  EJV
---------------------------------------------------------
A 556 3 1 1
A 557 1 1 1


[DataSrc] can have maximum of 4 different values.



Post #821947
Posted Thursday, November 19, 2009 2:28 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 6:26 PM
Points: 2,789, Visits: 4,131
You could have a look at the following articles referenced in my signature: CrossTab or Dynamic Cross Tab (if DataSrc contains an unknown list of elements).
I assume you're using SQL2000 (since posted in the SS2K forum). Otherwise I would have suggested to look into PIVOT.




Lutz

A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs
Post #821972
Posted Friday, November 20, 2009 8:30 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Tuesday, February 16, 2010 11:22 AM
Points: 549, Visits: 746
Dear lmu92,
Thank you for trying to help but ...
your article is very big. I didn't have time to search through it to find if it's relevant to my case.

There is a much better and shorter article at
http://www.databasejournal.com/features/mssql/article.php/3521101/Cross-Tab-reports-in-SQL-Server-2005.htm

written by Muthusamy Anantha Kumar aka The MAK.
This gave me a good clue.
Thank you Muthusamy Anantha Kumar!


Here is my solution:

drop table #BondSpread
create table #BondSpread
(
[Index] varchar(15),
CUSIP varchar(15),
Spread float,
DataSrc varchar(10)
)
insert #BondSpread ([Index],CUSIP,Spread,DataSrc) values ('UK_GEN_BBB','00998064',21.5447,'MHS')
insert #BondSpread ([Index],CUSIP,Spread,DataSrc) values ('UK_GEN_BBB','00998064',21.5447,'MHS')
insert #BondSpread ([Index],CUSIP,Spread,DataSrc) values ('UK_GEN_BBB','00998064',36.5447,'FRDOps')
insert #BondSpread ([Index],CUSIP,Spread,DataSrc) values ('C_IND_A','00998064',21.5447,'EJV')
insert #BondSpread ([Index],CUSIP,Spread,DataSrc) values ('C_IND_A','00998064',21.5447,'EJV')
insert #BondSpread ([Index],CUSIP,Spread,DataSrc) values ('C_IND_A','00998064',21.5447,'EJV')

select * from #BondSpread
/*
Index CUSIP Spread DataSrc
-----------------------------------------------------------------------------
UK_GEN_BBB 00998064 21.5447 MHS
UK_GEN_BBB 00998064 21.5447 MHS
UK_GEN_BBB 00998064 36.5447 FRDOps
C_IND_A 00998064 21.5447 EJV
C_IND_A 00998064 21.5447 EJV
C_IND_A 00998064 21.5447 EJV
*/

SELECT [Index],CUSIP,
COUNT(CASE DataSrc WHEN 'MHS' then 1 else null end) AS MHS,
COUNT(CASE DataSrc WHEN 'FRDOps' then 1 else null end) AS FRDOps,
COUNT(CASE DataSrc WHEN 'EJV' then 1 else null end) AS EJV,
COUNT(CASE DataSrc WHEN 'FTG' then 1 else null end) AS FTG
FROM #BondSpread
--where COB >= '11/18/2008' --and [Index] = 'A_BF_A' and CUSIP = 'EH007645'
GROUP BY [Index],CUSIP

/*
Index CUSIP MHS FRDOps EJV FTG
---------------------------------------------------------------------------------
C_IND_A 00998064 0 0 3 0
UK_GEN_BBB 00998064 2 1 0 0
*/




Post #822453
Posted Friday, November 20, 2009 10:39 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:56 PM
Points: 20,164, Visits: 13,699
BWAAA-HAAA!!!! Thanks for posting the answer you got but if you had posted data that way in your original post, you'd have had an answer on this forum almost immediately. Just incase you want a quick answer in the future, you should read how to do that... the URL is the first one in my signature below. If you're the same "riga" that I'm thinking of, you said you'd post correctly in the future and you haven't. If you're not the same "riga", then it's time you learned. Read the article I just pointed out.

Also, Lutz wouldn't have referred you to that other article unless he was absolutely sure that it would solve your problem. Now that you have a little time on your hands, you should go back and read it so you can figure this type of stuff out on your own.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #822835
« Prev Topic | Next Topic »


Permissions Expand / Collapse