Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Pivot Table - Multiple cols without dynamic query

Author  Topic 

imadiamond2016
Starting Member

29 Posts

Posted - 2012-06-07 : 16:37:18
I need to select the pivoted data without using dynamic queries as the they significantly reduce the performance. Is it possible? If not, then Would it be a good idea to embed the data in excel from the front end, create the Pivot there and retrieve the required data from excel and use in my grid?

In my actual scenario, I have 8 Data value columns to be pivoted.

Sample Data

Data To Pivot



Pivoted Data


robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-06-07 : 17:20:34
[code]SELECT Product, SUM(CASE WHEN Region='US' THEN Sale END) AS US_Sale,
SUM(CASE WHEN Region='Canada' THEN Sale END) AS Canada_Sale,
MAX(CASE WHEN Region='US' THEN DeliveryDate END) AS US_DeliveryDate,
MAX(CASE WHEN Region='Canada' THEN DeliveryDate END) AS Canada_DeliveryDate
FROM myTable
GROUP BY Product[/code]Just add the relevant CASE expressions for the remaining columns.
Go to Top of Page

imadiamond2016
Starting Member

29 Posts

Posted - 2012-06-08 : 02:28:50
I can't use CASE statement here as I don't know about the column list and user will be adding more and more column lists. What i presented here is just an example, actually I may have N number of columns list i.e. Counteries.
Go to Top of Page

vinu.vijayan
Posting Yak Master

227 Posts

Posted - 2012-06-08 : 05:48:55
I don't know if Multiple Fields can be Pivotted On the same Pivot condition.

So, I had to do a join between two Pivots and then group the data according to Product as follows:


--Creating Table

Create Table Ex
(Product varchar(10),
Region varchar(10),
Sales int,
DeliveryDate Date)


--Inserting Sample Data

Insert Into Ex Values('Cheese', 'US', 500, '2012/06/01')
Insert Into Ex Values('Eggs', 'US', 1100, '2012/06/10')
Insert Into Ex Values('Eggs', 'Canada', 1000, '2012/06/09')


--Query For Your Requirement

Select x.Product, Sum(x.US_Sales), SUM(x.Canada_Sales), MAX(x.US_DeliveryDate), MAX(x.Canada_DeliveryDate) From
(Select a.Product, a.US_Sales, a.Canada_Sales, b.US_DeliveryDate, b.Canada_DeliveryDate, Row_Number() Over (Partition By a.US_Sales Order By (Select NULL)) As rn From
(Select Product, [US] As US_Sales, [Canada] As Canada_Sales From Ex
Pivot
(Max(Sales) For Region In ([US], [Canada])) As pvt) As a
JOIN
(Select Product, [US] As US_DeliveryDate, [Canada] As Canada_DeliveryDate From Ex
Pivot
(Max(DeliveryDate) For Region In ([US], [Canada])) As pvt) As b ON a.Product = b.Product ) As x
Where x.rn = 1
Group By x.Product


Hope, this is what you are looking for.

N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-06-08 : 06:45:38
quote:
Originally posted by imadiamond2016

I can't use CASE statement here as I don't know about the column list and user will be adding more and more column lists. What i presented here is just an example, actually I may have N number of columns list i.e. Counteries.
So you need to dynamically pivot but can't use dynamic SQL? That's a neat trick I'd like to see. (seriously)

As I mentioned in your other post, pivoting is best done on the client side with a reporting tool. There's no magic to it in SQL Server, you can either construct fixed pivot columns or use dynamic SQL to construct it dynamically.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-08 : 12:13:39
quote:
Originally posted by imadiamond2016

I can't use CASE statement here as I don't know about the column list and user will be adding more and more column lists. What i presented here is just an example, actually I may have N number of columns list i.e. Counteries.


i would have definitely done it using a reporting tool then. As Rob suggested, most reporting tools you can make it do pivoting based on field dynamically by applying a column group based on it

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -