PIVOT without defining the Column headers

I've got a stored procedure that generates a Pivot table as a result set based on certain parameters I pass to the SP.  However, I would like to create a view of that data without having to define the column headers for the pivot columns.

My concept would be to include a WHERE clause in the subquery that only pulls data from the previous 30 days, something like:

SELECT Entity_ID, docDate, [Gas]
FROM myTable WHERE docDate >= '2015-03-01'
) as T
FOR [docDate]
) as pvt

without the IN clause in the PIVOT clause.  And the output would look something like:

Entity_ID   [2015-03-01]   [2015-03-02]  [2015-03-03]  ...  [2015-03-19]
     1                   25                      27                       24                         30
     2                  100                     95                     117                       110
LVL 50
Dale FyeAsked:
Who is Participating?
You have to specify the column values that turn into columns as column headers and dates will change even if you do the last 30 days. You can't avoid that unless you use a dynamic query.
Dale FyeAuthor Commented:
D...! I was looking for a way to do this in a view.  I've already already got the dynamic SP, but need this in a view that i can join to other tables or views.
If you can live with having D-30 , D-29 .... D-01 as column names you can do it in a view or make it a table valued function.  Transform the date in it's D minus x equivalent string
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Éric MoreauSenior .Net ConsultantCommented:
SQL is only able to do static pivot (where the columns name are know ahead of time).

What you need is a dynamic pivot. Using a bit of dynamic queries, you can build a dynamic pivot. Have a look at this sample: http://sqlhints.com/2014/03/18/dynamic-pivot-in-sql-server/
Can you give us a sample of data that you have in MyTable?
Dale FyeAuthor Commented:

It looks something like:

Entity_ID   docDate              [Gas]
1                 2015-03-01           25
1                 2015-03-02           27
1                 2015-03-03           24
1                 2015-03-19           30
2                 2015-03-01           100
2                 2015-03-02           95
2                 2015-03-03           117
2                 2015-03-19           110
Dale FyeAuthor Commented:

I already have a stored procedure that does this dynamically, with values passed in for the startdate and enddate, but was hoping there was a way to do it without explicitly identifying all of the columns before hand, as in Access.
Éric MoreauSenior .Net ConsultantCommented:
As I already said, SQL Server is only able to do static pivot.
Dale FyeAuthor Commented:
Thanks, guys.  Not the answer I wanted, but what I expected.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.