?
Solved

SQL PIVOT With Dynamic List...

Posted on 2008-10-09
3
Medium Priority
?
371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1500 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 60

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

770 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