Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Pivot Table In SQL Server 2005

Author  Topic 

mustbemikey
Starting Member

1 Post

Posted - 2008-10-07 : 13:09:09
Greatings Everyone,



I posted a question about getting pivot table like results from sql server 2000. We have upgraded to sql server 2005 and I would really like to know how to use the pivot table functionality. I have seen other pivot table examples but I don't know how to use them for my needs and using my tables.



What I want to do is have the result look like this: (The results are based on a category, the result below is given the category of wireless)



Result (bellow)



Wireless:

Part Number | Description | Manufacturer | Price | FCC Certified | Frequency | Protocol

1 | Part1 | Sony | 1.00 | [blank] |900-930 | DTS
3 | Part3 | LG | 5.16 | N | [blank] | FHSS



Here are the two table that I would have to pull data from:



Table 1: Items_Published

Table 2: Menu_Selected



Items_Published:

Part_Number | Description | Manufacturer | Price

1 | Part1 | Sony | 1.00
2 | Part2 | Sony | 3.14
3 | Part3 | LG | 5.16
4 | Part4 | LG | 3.02
5 | Part5 | Sony | 0.56



Menu_Selected:

Category | Spec | Manufacturer | SpecValue | Part_Number

Wireless | Frequency | Sony | 900-930 | 1

Wireless | Protocol | Sony | DTS | 1

MCU | FCC Certified | LG | Y | 2

Wireless | FCC Certified | LG | N | 3

Wireless | Protocol | LG | FHSS | 3





Thank you for your responses. Always helpful!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-07 : 13:28:53
[code]SELECT *
FROM
(SELECT p.Part_Number
p.Description,
p.Manufacturer,
p.Price,
m.Spec,
m.SpecValue
FROM Items_Published p
INNER JOIN Menu_Selected m
ON m.Part_Number =p.Part_Number
WHERE m.Category='Wireless') t
PIVOT (MAX(SpecValue) FOR Spec IN ([FCC Certified],[Frequency],[Protocol]))tmp[/code]
Go to Top of Page
   

- Advertisement -