Solved

SQL PIVOT With Dynamic List...

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can i use insert and update together by merging two tables? 9 30
SQL STANDARD CORE 8 40
SQL Syntax Grouping Sum question 7 27
Where is the Help Section? 8 28
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

830 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