UnPivot in sql server

  • Hi All,

    My requirement as follows,

    Table Structure :

    CropCountSeedCountRetSalesDisSales

    2 3 100 200

    3 7 400 800

    Desired Output:

    Type Count Sales

    CropCount 5 0

    SeedCount 10 0

    RetSales 0 500

    DiSales 0 1000

  • Hi all i got the solution:

    Select Type,Sum(Count)Count,0 as sales From (select Type,Count from #test unpivot(Count for Type in([cropcnt],[SeedCnt])) as upvt1)b

    Group by Type

    union all

    Select Type,0 as Count,Sum(Sales)Sales From (select Type,sales from #test unpivot(Sales for Type in([RetSales],DisSales)) as upvt1)b

    Group by Type

Viewing 2 posts - 1 through 1 (of 1 total)

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