SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Converting particular set of Rows as Columns


Converting particular set of Rows as Columns

Author
Message
gadde
gadde
SSC-Enthusiastic
SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)

Group: General Forum Members
Points: 192 Visits: 113
Converting one row as one Column is possible using Pivot. But is there any way to convert particular set of rows to one column?
For example, below is the data table

Customer---------------Product-------------Qty
Jain --------------- Mango -------------10
Charlie --------------- Orange--------------5
Rocky --------------- Mango---------------6
Rocky ---------------- Orange---------------3

Expected Result set

Customer---------------Mango---------------Orange---------------Mango and Orange
Jain---------------------10-------------------NULL ---------------------NULL
Charlie------------------NULL-------------------5-----------------------NULL
Rocky -------------------NULL-----------------NULL---------------------- 9

Appreciate any help

* ---- are used for identation purpose only

Thank You :-)
Mansfield
Mansfield
Right there with Babe
Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)Right there with Babe (718 reputation)

Group: General Forum Members
Points: 718 Visits: 394
Can you please provide a detailed example with table scripts, sample data, and the expected results like this? Also, can you please describe how are the sets of rows determined (e.g. is it always going to be "Mango and Orange"?)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)

Group: General Forum Members
Points: 221638 Visits: 42003
gadde (5/1/2013)
Converting one row as one Column is possible using Pivot. But is there any way to convert particular set of rows to one column?
For example, below is the data table

Customer---------------Product-------------Qty
Jain --------------- Mango -------------10
Charlie --------------- Orange--------------5
Rocky --------------- Mango---------------6
Rocky ---------------- Orange---------------3

Expected Result set

Customer---------------Mango---------------Orange---------------Mango and Orange
Jain---------------------10-------------------NULL ---------------------NULL
Charlie------------------NULL-------------------5-----------------------NULL
Rocky -------------------NULL-----------------NULL---------------------- 9

Appreciate any help

* ---- are used for identation purpose only

Thank You :-)


What you have there is actually a Pivot or, in old terms, a Cross Tab. Here's a couple of articles that explain (step by step in simple terms) how to hardcode one and how to do it dynamically on the other.

http://www.sqlservercentral.com/articles/T-SQL/63681/
http://www.sqlservercentral.com/articles/Crosstab/65048/

--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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)

Group: General Forum Members
Points: 221638 Visits: 42003
Ah... sorry. Might not be right for you. Didn't see where you doubled up on "Rocky". the dynamic SQL article I previously mentioned might help, though.

--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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
gadde
gadde
SSC-Enthusiastic
SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)SSC-Enthusiastic (192 reputation)

Group: General Forum Members
Points: 192 Visits: 113
Hi All,

Thanks for the help.
Got it Smile

Below is the query:

select * from
(select customer
,sum(qty) qty1
,(select cast(product as varchar(max)) from datatable where customer = a1.customer for XML path(''))product
from datatable a1
group by customer
)dataforpivot_table
pivot (sum(qty1) for product in ([Mango],[Orange],[MangoOrange]))pivoted;

Products are not fixed, they keep on changing, and even the combinations change.
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