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


Add to briefcase

convert Row to column. Expand / Collapse
Author
Message
Posted Tuesday, January 18, 2011 2:05 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, May 29, 2012 6:10 AM
Points: 5, Visits: 38

Hi,

How to convert row to column for the below eg. I used pivot to do this. But i am getting only one account for an ACMnumber.
But i need 3 records for an account with group by dataset as per below eg. i Have given a eg of one ACMnumber, but i have millions of ACMnumber in that table .
Can any one give me a query to do this.
Also given the eg query which i tried.



Table: vacmvariables

ACMnumber Dataset Stringvalue
153588 custAcctNo 6U7000000
153588 custAcctNo 6Y0000001
153588 custAcctNo 6Y0000002
153588 custID 443
153588 accountTitle IRA FBO Loelei C Dehart DecD
153588 accountTitle Martha Johnson
153588 accountTitle William C Kearns & Joan
153588 shortName W00001
153588 shortName P00009
153588 shortName P00003

Desired OUTPUT

custAcctNo custID accountTitle shortName
6U7000000 443 IRA FBO Loelei C Dehart DecD W00001
6Y0000001 Martha Johnson P00009
6Y0000002 William C Kearns & Joan P00003



Query i tried

select custAcctNo,custID,shortName,accountTitle
from
(select Acmnumber, dataset, stringvalue from vacmvariables
where acmnumber = 153588)
up pivot ( max (stringvalue ) for dataset in ( custAcctNo,custID,shortName,accountTitle)) as pvt
order by acmnumber

Need smillar kind of query to return 3 rows as desired above.
Post #1049133
Posted Tuesday, January 18, 2011 3:45 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 2:33 AM
Points: 985, Visits: 2,563
hi,

How do you know that "6U7000000 443 IRA FBO Loelei C Dehart DecD W00001" is 1 row and not any other combination?

Try to add row_number to the query, the row number will give you another group by field.
something like this

select custAcctNo,custID,shortName,accountTitle
from
(select Acmnumber, dataset, stringvalue, row_number() over (partition by Acmnumber, dataset order by FIELD) as rnum from vacmvariables
where acmnumber = 153588)
up pivot ( max (stringvalue ) for dataset in ( custAcctNo,custID,shortName,accountTitle)) as pvt
order by acmnumber

Post #1049171
Posted Tuesday, January 18, 2011 6:01 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, May 29, 2012 6:10 AM
Points: 5, Visits: 38
Thanks,
This is what i need.
How does rownum aggregates from 10 rows to 3 rows. can you explain.
Post #1049258
Posted Tuesday, January 18, 2011 7:46 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 2:33 AM
Points: 985, Visits: 2,563
Hi,

I'm happy that I can help.
The row number help with cause we have a new column to group by. If you'll add the rnum to the outer select you'll see how he effects the query.

Post #1049359
Posted Wednesday, October 9, 2013 7:34 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 9, 2013 7:38 AM
Points: 1, Visits: 1
I need some assistance....I have the screen shot attached below. I would like the output to have column headers 'vessel' and each of the twelve months. I would like to have a 'po number' count for every month for each 'vessel'


  Post Attachments 
PIVOT.jpg (11 views, 363.02 KB)
Post #1503102
Posted Wednesday, October 9, 2013 7:39 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, September 19, 2014 2:34 PM
Points: 12,923, Visits: 12,342
TRACIEJOHNSON30 (10/9/2013)
I need some assistance....I have the screen shot attached below. I would like the output to have column headers 'vessel' and each of the twelve months. I would like to have a 'po number' count for every month for each 'vessel'


Hi and welcome to the forums. It is best if you start your own thread instead of hijacking a 2 year old thread. Also, this is a sql server site, not a MySql site, so you will find that the syntax we come up with will work for sql server. It may not work for MySql. Last but not least, on any technical forum where you post for help you need to provide enough information for people to be able to help. A screen shot of a query does not offer much in the way of detail. You should instead include:

1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1503104
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse