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 52
Dale FyeOwner, Developing Solutions LLCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dale FyeOwner, Developing Solutions LLCAuthor 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
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

É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 FyeOwner, Developing Solutions LLCAuthor 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 FyeOwner, Developing Solutions LLCAuthor 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 FyeOwner, Developing Solutions LLCAuthor Commented:
Thanks, guys.  Not the answer I wanted, but what I expected.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.