Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2004
    Posts
    139

    Unanswered: simple pivot sql server 2005

    Is there a way to include multiple aggregations in one pivot statement?

    For example:

    select
    category_cd,
    [a]
    from
    (
    select
    category_cd,
    status_cd,
    balance
    from
    table
    ) as t
    pivot
    (
    sum(balance) for status_cd in
    (
    [a]
    )
    ) as p

    will return:

    category_cd a
    1 2399059.92
    2 174310512.4
    3 2211823370
    4 1564431422

    But I'd also like to get a count for the pivot column. Can anyone point me in the right direction. Thank you.
    Thanks,
    Bill

  2. #2
    Join Date
    Jan 2008
    Location
    Billings, MT
    Posts
    14
    Why not just use SUM and COUNT? If this is a simplified example, you'll probably have to use CASE statements and forgo PIVOT altogether. That's not all that bad if you've got a small number of pivoted columns.
    Tom Rupsis
    Granite Peak Systems
    Phone: 406-672-8292
    Email: trupsis@granitepeaksys.com
    LinkedIn: www.linkedin.com/in/trupsis

Posting Permissions

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