Solved

SQL PIVOT With Dynamic List...

Posted on 2008-10-09
3
353 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

932 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

10 Experts available now in Live!

Get 1:1 Help Now