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 T.Entity_ID
FROM (
SELECT Entity_ID, docDate, [Gas]
FROM myTable WHERE docDate >= '2015-03-01'
) as T
PIVOT (
SUM([Gas])
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?
 
ZberteocCommented:
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.
0
 
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.
0
 
jogosCommented:
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
0
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/
0
 
ZberteocCommented:
Can you give us a sample of data that you have in MyTable?
0
 
Dale FyeAuthor Commented:
Zberteoc,

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
0
 
Dale FyeAuthor Commented:
Eric,

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.
0
 
Éric MoreauSenior .Net ConsultantCommented:
As I already said, SQL Server is only able to do static pivot.
0
 
Dale FyeAuthor Commented:
Thanks, guys.  Not the answer I wanted, but what I expected.
0
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.