Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


convert Row to column.


convert Row to column.

Author
Message
dpalani
dpalani
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 39
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.
kupy
kupy
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1074 Visits: 2647
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


dpalani
dpalani
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 39
Thanks,
This is what i need.
How does rownum aggregates from 10 rows to 3 rows. can you explain.
kupy
kupy
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1074 Visits: 2647
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.
TRACIEJOHNSON30
TRACIEJOHNSON30
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
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'
Attachments
PIVOT.jpg (12 views, 363.00 KB)
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16545 Visits: 16999
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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search