Multiple aggregates using PIVOT

  • 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

  • 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"

  • I don't want to use CASE statement. I'd like to know how to do this using PIVOT.


    Kindest Regards,

    Hari

  • 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"

  • 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

  • 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

  • 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"

  • 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>

    😀

  • It works somehow.

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply