Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Multiple aggregates using PIVOT Expand / Collapse
Author
Message
Posted Tuesday, August 28, 2007 9:03 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, September 13, 2012 10:27 PM
Points: 47, Visits: 31

Hi all,

Am working on a reporting project which extensively uses the backend stored procedures. We're using SQL 2005 as database.

I have a table as shown below:

Product NamePart NumberBOM DateQtyExtended Cost
BOM1SA2135A-516311/10/2005 0:0011.50000
BOM1T673ZG11/10/2005 0:0012.50000
BOM14870370A4911/10/2005 0:0021.25000
BOM10109060A5811/10/2005 0:0011.00000
BOM14809948D3011/10/2005 0:0011.16000
BOM12409414M4211/10/2005 0:0010.02790
BOM12409154M6211/10/2005 0:0010.00486
BOM12409154M6411/10/2005 0:0010.00486
BOM12409154M3611/10/2005 0:001NULL
BOM12409154M5111/10/2005 0:0010.00486
BOM2SA2616A-75205/10/2007 0:001NULL
BOM25688653L775/10/2007 0:000.10.03222
BOM2SNN5696B5/10/2007 0:0012.84225
BOM26802919J035/10/2007 0:001NULL
BOM2SHN8962A5/10/2007 0:0010.86545
BOM21189975Y015/10/2007 0:001NULL
BOM21590167N055/10/2007 0:0010.98714
BOM2SKN6371C5/10/2007 0:0010.24500
BOM23087629N035/10/2007 0:001NULL
BOM2T676KD5/10/2007 0:001NULL

The desired output is:

 BOM1BOM2
Part NumberSum of QtySum of Extended CostSum of QtySum of Extended Cost
1189975Y011013
1590167N0510.98714286111.5
SHN8962A10.86545002512
SA3010A-63821011
5664750E620.050.0444236760.051.25
CHUG1422BD1011.3
SJUG0866CA1010
7109003A501011
SHN8965A1011
1487942Y0110.03510.035

There needs to be 2 aggregate columns for each BOM. I tried to acheive the partial result with the query as shown below:
 
SELECT [PART NUMBER],[BOM DATE],[BOM1],[BOM2]
FROM
(
     SELECT [PRODUCT NAME],[PART NUMBER],[BOM DATE],[QUANTITY]
     FROM [CURRENT BOM] WHERE [PRODUCT NAME] IN ('BOM1','BOM2')
) AS DATA
PIVOT (SUM([QUANTITY]) FOR [PRODUCT NAME] IN ([BOM1],[BOM2])) AS PVT
 
But in the PIVOT option we can only specify one aggregate column. How do I get two aggregate columns? something like SUM([QUANTITY]),SUM([EXT_COST])..
I know its not allowed, but I would like to know if there is any work around.
 
I'm really stuck with this issue and not able to get any help from the web.
 
I'd really appreciate if any of you can help on this at the earliest.
 
Tonnes of thanks in advance,
 
Hari

 




Kindest Regards,

Hari

Post #394342
Posted Tuesday, August 28, 2007 11:31 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 1:50 AM
Points: 2,397, Visits: 3,408

SELECT [Part Number],
SUM(CASE WHEN Product = 'BOM1' THEN ISNULL(Qty, 0) ELSE 0 END) AS Bom1Qty,
SUM(CASE WHEN Product = 'BOM1' THEN ISNULL([Extended Cost], 0) ELSE 0 END) AS Bom1Cost,
SUM(CASE WHEN Product = 'BOM2' THEN ISNULL(Qty, 0) ELSE 0 END) AS Bom2Qty,
SUM(CASE WHEN Product = 'BOM2' THEN ISNULL([Extended Cost], 0) ELSE 0 END) AS Bom2Cost
FROM Table1
GROUP BY [Part Number]
ORDER BY [Part Number]

 




N 56°04'39.16"
E 12°55'05.25"
Post #394408
Posted Tuesday, August 28, 2007 6:27 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, September 13, 2012 10:27 PM
Points: 47, Visits: 31
I don't want to use CASE statement. I'd like to know how to do this using PIVOT.


Kindest Regards,

Hari

Post #394542
Posted Wednesday, August 29, 2007 2:14 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 1:50 AM
Points: 2,397, Visits: 3,408

Good luck!

Most often, the CASE statements are faster than the PIVOT too, in my experience.

 




N 56°04'39.16"
E 12°55'05.25"
Post #394598
Posted Wednesday, August 29, 2007 5:22 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, September 19, 2007 3:33 AM
Points: 20, Visits: 1
A combination between the 2 solution can do as well :>)


SELECT *
FROM
(
SELECT [PRODUCT_NAME],[PART_NUMBER],[Qty],
sum(case when Product_Name = 'BOM1' then isnull(Extended_Cost,0) else 0 end ) as Bom1Cost,
sum(case when Product_Name = 'BOM2' then isnull(Extended_Cost,0) else 0 end ) as Bom2Cost
FROM [CURRENTBOM] WHERE [PRODUCT_NAME] IN ('BOM1','BOM2')
group by [PRODUCT_NAME],[PART_NUMBER],[Qty]
) AS DATA
PIVOT
(SUM([Qty]) FOR [PRODUCT_NAME] IN ([BOM1],[BOM2])) as PVT1
order by
Part_Number

Post #394647
Posted Wednesday, August 29, 2007 8:32 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, September 13, 2012 10:27 PM
Points: 47, Visits: 31
thanks for the reply. the problem is the number of bom names used in the query is dynamic ie., the product names are not limited to 2. even the query i have in my post is a dynamically formed sql statement.


Kindest Regards,

Hari

Post #394750
Posted Wednesday, August 29, 2007 8:42 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 1:50 AM
Points: 2,397, Visits: 3,408

Why didn't you say so from the beginning?

Read my article here http://www.sqlservercentral.com/columnists/plarsson/pivottableformicrosoftsqlserver.asp

It has everything you need, including multiple aggregations.

 




N 56°04'39.16"
E 12°55'05.25"
Post #394757
Posted Sunday, April 13, 2014 7:36 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, April 13, 2014 7:35 AM
Points: 3, Visits: 8
SwePeso (8/28/2007)
<FONT color=#0000ff size=2>

SELECT</FONT><FONT size=2> [Part Number]</FONT><FONT color=#808080 size=2>,
</FONT><FONT color=#ff00ff size=2>SUM</FONT><FONT color=#808080 size=2>(</FONT><FONT color=#0000ff size=2>CASE</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>WHEN</FONT><FONT size=2> Product </FONT><FONT color=#808080 size=2>=</FONT><FONT size=2> </FONT><FONT color=#ff0000 size=2>'BOM1'</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>THEN</FONT><FONT size=2> ISNULL(Qty, 0) </FONT><FONT color=#0000ff size=2>ELSE</FONT><FONT size=2> 0 </FONT><FONT color=#0000ff size=2>END</FONT><FONT color=#808080 size=2>)</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>AS</FONT><FONT size=2> Bom1Qty</FONT><FONT color=#808080 size=2>,
</FONT><FONT color=#ff00ff size=2>SUM</FONT><FONT color=#808080 size=2>(</FONT><FONT color=#0000ff size=2>CASE</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>WHEN</FONT><FONT size=2> Product </FONT><FONT color=#808080 size=2>=</FONT><FONT size=2> </FONT><FONT color=#ff0000 size=2>'BOM1'</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>THEN</FONT><FONT size=2> ISNULL([Extended Cost], 0) </FONT><FONT color=#0000ff size=2>ELSE</FONT><FONT size=2> 0 </FONT><FONT color=#0000ff size=2>END</FONT><FONT color=#808080 size=2>)</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>AS</FONT><FONT size=2> Bom1Cost</FONT><FONT color=#808080 size=2>,
</FONT><FONT color=#ff00ff size=2>SUM</FONT><FONT color=#808080 size=2>(</FONT><FONT color=#0000ff size=2>CASE</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>WHEN</FONT><FONT size=2> Product </FONT><FONT color=#808080 size=2>=</FONT><FONT size=2> </FONT><FONT color=#ff0000 size=2>'BOM2'</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>THEN</FONT><FONT size=2> ISNULL(Qty, 0) </FONT><FONT color=#0000ff size=2>ELSE</FONT><FONT size=2> 0 </FONT><FONT color=#0000ff size=2>END</FONT><FONT color=#808080 size=2>)</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>AS</FONT><FONT size=2> Bom2Qty</FONT><FONT color=#808080 size=2>,
</FONT><FONT color=#ff00ff size=2>SUM</FONT><FONT color=#808080 size=2>(</FONT><FONT color=#0000ff size=2>CASE</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>WHEN</FONT><FONT size=2> Product </FONT><FONT color=#808080 size=2>=</FONT><FONT size=2> </FONT><FONT color=#ff0000 size=2>'BOM2'</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>THEN</FONT><FONT size=2> ISNULL([Extended Cost], 0)</FONT><FONT color=#0000ff size=2>ELSE</FONT><FONT size=2> 0 </FONT><FONT color=#0000ff size=2>END</FONT><FONT color=#808080 size=2>)</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>AS</FONT><FONT size=2> Bom2Cost
</FONT><FONT color=#0000ff size=2>FROM</FONT><FONT size=2> Table1
</FONT><FONT color=#0000ff size=2>GROUP</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>BY</FONT><FONT size=2> [Part Number]
</FONT><FONT color=#808080 size=2>ORDER</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>BY</FONT><FONT size=2> [Part Number]</FONT>

<FONT size=2></FONT>
Post #1561275
Posted Sunday, April 13, 2014 7:37 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, April 13, 2014 7:35 AM
Points: 3, Visits: 8
It works somehow.
Post #1561276
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse