Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2004
    Location
    Charlotte, NC
    Posts
    79

    Unanswered: Aggregate two different ways from one table

    Hi

    I have a table with these columns:

    rpt_week
    event
    channel
    rpt_product_group
    purpose
    volume
    mkt_volume

    channel can have various values, but there are 3 I am interested in getting - 'centralized', 'decentralized' and 'retail'.

    For each combination of rpt_week, event, rpt_product_group and purpose I want to get the SUM of volume and mkt_volume.

    The catch is, I only want the SUM of volume where channel = 'centralized', but I want the SUM of mkt_volume for all 3 channels above.

    Can someone help point me in the right direction with this?

    Thanks
    Make something idiot proof and someone will make a better idiot...

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    SELECT rpt_week
         , event
         , rpt_product_group
         , purpose
         , SUM(CASE WHEN channel ='centralized'
                    THEN volume ELSE NULL END) AS centralized_volume
         , SUM(mkt_volume) AS sum_mkt_volume
    
      FROM daTable
    GROUP
        BY rpt_week
         , event
         , rpt_product_group
         , purpose
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Feb 2004
    Location
    Charlotte, NC
    Posts
    79
    If you could see me now you'd witness me slapping myself on my forehead for not seeing that one. DOH!

    Thanks for this, that's exactly what I need.
    Make something idiot proof and someone will make a better idiot...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •