We help IT Professionals succeed at work.
Get Started

Pivoting SQL in ASP with multiple key columns

Last Modified: 2012-05-07
Hi All,

I'm currently building a dynamic reporting tools for our stores, all is going well - my code is correctly building an SQL query to match all selections, retrieving the data, and putting it into a datatable.

Pending their selection, i need to pivot the data - for example, on the date column, to show monthly sales for each product across a row. I am familiar with how to pivot in SQL, a simple example here:


I have all of the required data, grouped and summed fully via the original query, and loaded into a datatable for easy binding to a gridview. Is it possible to use datatable.createreader() to create a datareader, and then use standard SQL to pivot the data? How would i do this?

I have found pivot functions online that handle the pivot well when there is only 1 key column, but my results may have many key's, such as breakdowns from Manufacturer, to product line, to actual product, etc.

TL;DR: Can I/How do I run standard SQL as per the above link, on a asp.net C# Datatable?

Otherwise who wants to write a pivot function that supports multiple key columns!:)

Thanks, Charles
Watch Question
Chief Technology Officer
Most Valuable Expert 2011
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE