Solved

SQL PIVOT With Dynamic List...

Posted on 2008-10-09
3
349 Views
Last Modified: 2012-05-05
I've the following pivot statement...

SELECT PartType, PartTypeSize, [000], [050], [075], [100]
FROM (SELECT     PartType, PartTypeSize, Op_Num, SerialNumber FROM vWIP) AS S
PIVOT (Count(SerialNumber) FOR Op_Num IN ([000], [050], [075], [100]) AS D

The values for [000], [050], [075], [100] are pretty crude.  But it works for this example.  Is there a way to dynamically build these values based upon a select statement.  More like...

SELECT PartType, PartTypeSize, (SELECT DISTINCT values FROM table WHERE condition)
FROM (SELECT     PartType, PartTypeSize, Op_Num, SerialNumber FROM vWIP) AS S
PIVOT (Count(SerialNumber) FOR Op_Num IN (SELECT DISTINCT values FROM TABLE WHERE condition) AS D

I figured that I can do this in software and dynamically build the statement. but currently I've this defined in a database view and would like the values to be based upon a changing table.

Possible?  probably not...  Asking for too much probably


0
Comment
Question by:allidap
  • 2
3 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22682755
You can do it in SQL in the same way you would in software using dynamic SQL statement.  You would query and build a string representing your SQL statement with hardcoded column names (except you didn't have to hand type them) and you execute like this:

EXEC(@dynamicSQL)
0
 

Author Comment

by:allidap
ID: 22694565
Thanks for the help.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22694845
You are welcome.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now