User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 456,234 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,762 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser: Programming Forums
Views: 5227 | Replies: 2 | Solved
Reply
Join Date: Nov 2007
Posts: 2
Reputation: Thanatoid is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Thanatoid Thanatoid is offline Offline
Newbie Poster

PIVOT TABLE Dynamic Column Header?

  #1  
Nov 14th, 2007
I am trying to work on a database with 3 tables. To make it easier I have created a couple of temp tables to work out the syntax.

CREATE TABLE #owner
(
[NameId] tinyint IDENTITY(1,1) NOT NULL,
[Name] varchar(50) NOT NULL
)

INSERT INTO #owner VALUES ('Thanatoid');
INSERT INTO #owner VALUES ('Other');

CREATE TABLE #propertyType
(
[TypeId] tinyint IDENTITY(1,1) NOT NULL,
[Name] varchar(50) NOT NULL
)

INSERT INTO #propertyType VALUES ('Home');
INSERT INTO #propertyType VALUES ('Car');

CREATE TABLE #property
(
[NameId] tinyint NOT NULL,
[TypeId] tinyint NOT NULL,
[Value] varchar(50) NOT NULL
)

INSERT INTO #property VALUES (1,1, 'Blue');
INSERT INTO #property VALUES (1,2, 'Black');
INSERT INTO #property VALUES (2,1, 'Red');
INSERT INTO #property VALUES (2,2, 'Black');

DROP TABLE #owner;
DROP TABLE #propertyType;
DROP TABLE #property

| NameId | Name |
| 1 | Thanatoid|
| 2 | other |

| TypeId | Name |
| 1 | Home |
| 2 | Car |

| NameId | TypeId | Value |
| 1 | 1 | Blue |
| 1 | 2 | Black |
| 2 | 1 | Red |
| 2 | 2 | Black |

Where property value is some arbitrary detail. The real propertyType has 50 or 60 rows and not every property has all of the values. I am trying to create a pivot table that would look like so that I can query the data and present the data easier:

[Owner | Home | Car ]
[Thanatoid | Blue | Black ]
[Other| Red | Black ]

The propertyTypes are added often, and I don't really have the ability to change them. There is a unique constrant on property on nameid and typeid so there will never be two of the same property with the same owner. Any help would be very helpful.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2008
Posts: 4
Reputation: luciano_sunny is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
luciano_sunny luciano_sunny is offline Offline
Newbie Poster

Re: PIVOT TABLE Dynamic Column Header?

  #2  
Feb 26th, 2008
This thread has been marked solved so it means you know now how to do it. I am stuck with something almost like this. If you have solved this can you please tell me how to.
Thanks.
Reply With Quote  
Join Date: Nov 2007
Posts: 2
Reputation: Thanatoid is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Thanatoid Thanatoid is offline Offline
Newbie Poster

Re: PIVOT TABLE Dynamic Column Header?

  #3  
Mar 3rd, 2008
Originally Posted by luciano_sunny View Post
This thread has been marked solved so it means you know now how to do it. I am stuck with something almost like this. If you have solved this can you please tell me how to.
Thanks.


What are you trying to do. What I did was this:
SET @pt = STUFF(
(
SELECT CAST(',' AS VARCHAR(MAX)) + CAST(QUOTENAME([ProfileItemType].[Name]) AS VARCHAR(MAX))
FROM [Table]

FOR XML PATH('')
), 1, 1, '')

SET @sql = '
SELECT *
FROM
(
INNER SELECT HERE
) AS t
PIVOT
(
MIN([Value])
FOR pt_name IN (' + @pt + N')
) AS pvt'

EXEC sp_executesql @sql;
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb MS SQL Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 5:06 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC