SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Multiple aggregates using PIVOT


Multiple aggregates using PIVOT

Author
Message
Hariharan-202345
Hariharan-202345
SSC-Enthusiastic
SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)

Group: General Forum Members
Points: 157 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

SwePeso
SwePeso
SSCrazy Eights
SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)

Group: General Forum Members
Points: 9579 Visits: 3433

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"
Hariharan-202345
Hariharan-202345
SSC-Enthusiastic
SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)

Group: General Forum Members
Points: 157 Visits: 31
I don't want to use CASE statement. I'd like to know how to do this using PIVOT.


Kindest Regards,

Hari

SwePeso
SwePeso
SSCrazy Eights
SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)

Group: General Forum Members
Points: 9579 Visits: 3433

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"
robert dragoi
robert dragoi
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 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
Hariharan-202345
Hariharan-202345
SSC-Enthusiastic
SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)

Group: General Forum Members
Points: 157 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

SwePeso
SwePeso
SSCrazy Eights
SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)

Group: General Forum Members
Points: 9579 Visits: 3433

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"
samjoseph_p
samjoseph_p
Valued Member
Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)

Group: General Forum Members
Points: 55 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>Wink</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>Wink</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>Wink</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>Wink</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>
:-D
samjoseph_p
samjoseph_p
Valued Member
Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)

Group: General Forum Members
Points: 55 Visits: 8
It works somehow.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search