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


Pivot query


Pivot query

Author
Message
FANINDRA BHORTAKKE
FANINDRA BHORTAKKE
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 24
Hi All,

I have two tables say Product and ProductDetails as follows:

Product
------------
ID Name
-----------
1 Mango
2 Orange
-----------

ProductDetails

ID Type Code
---------------
1 1 abcd
1 2 efgh
1 3 sdkl
2 1 mkcl
--------------------

I want to design query so that it gives me result:
---------------------------------------
ID Name Code1 Code2 Code3..........
---------------------------------------
1 Mango abcd efgh sdkl
2 Orange mkcl NULL NULL

---------------------------------




Thanks in Advance,
Fanindra
Garadin
Garadin
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2795 Visits: 4107
Check the article in my signature about crosstabs/pivots. It explains how to make a crosstab query which will work in 2000.

Also, you might want to take a peek at the post on how to provide sample data for future inquiries.

Seth Phelabaum
Consistency is only a virtue if you're not a screwup. ;-)

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40000 Visits: 38564
Garadin (3/3/2010)
Check the article in my signature about crosstabs/pivots. It explains how to make a crosstab query which will work in 2000.

Also, you might want to take a peek at the post on how to provide sample data for future inquiries.


I have to agree with Seth on both counts.

Seth, Haven't seen you around in a while, hope things have been okay.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Garadin
Garadin
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2795 Visits: 4107
Nice to see you too Lynn.

Things have been fine, I've just been really busy. Too busy working to post during the day, too busy playing CoD4 to post at home Hehe.

Seth Phelabaum
Consistency is only a virtue if you're not a screwup. ;-)

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
vax750(Hyung-Joo Kim)
vax750(Hyung-Joo Kim)
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 251
-- dynamic sql

/*

select *
into #Product
from (
select 1 ID, 'Mango' Name union all
select 2 ID, 'Orange' Name union all
select 3 ID, 'Apple' Name ) r



select *
into #ProductDetails
from (
select 1 ID, 1 Type ,'abcd' Code union all
select 1 ID, 2 Type ,'efgh' Code union all
select 1 ID, 3 Type ,'sdkl' Code union all
select 2 ID, 1 Type ,'mkcl' Code union all
select 3 ID, 50 Type ,'fgfdd' Code

) r

*/

declare @sql varchar(2000)

set @sql = 'select a.id , b.Name '

select @sql = @sql + ', max( case when a.type = ' + convert(varchar, (number + 1 ) ) + ' then a.code else '''' end) [code' + + convert(varchar, (number + 1 ) ) + ']'
from master.dbo.spt_values where type = 'P'
and number < ( select MAX(Type) from #ProductDetails )

select @sql = @sql + ' from #ProductDetails a , #Product b where a.id = b.id group by a.id , b.Name'

exec(@sql )

..
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