Query Help

  • I have the following table schema

    ReportCounty ( ID, ReportID, CountyID, Price )

    Now i want the following output

    ReportID County1  Couty2   Couty3 ......

    1  Price  Price  Price

    2  Price  Price  Price

    3  Price  Price  Price

    So on.

    Pls help me in making the query

    Prashant Thakwanithakwani_prashant@yahoo.co.in

  • select ReportId,

    SUM( CASE WHEN CountryId = 1 THEN PRICE ELSE 0 END) AS COUNTRY1 ,

    SUM( CASE WHEN CountryId = 2 THEN PRICE ELSE 0 END) AS COUNTRY2 ,

    SUM( CASE WHEN CountryId = 3 THEN PRICE ELSE 0 END) AS COUNTRY3

    from ReportCountry

    GROUP BY ReportId

     

    Add as many CASE as you want. If you are not sure how many, switch to dynamic SQL.

     

     


    -- Amit



    "There is no 'patch' for stupidity."



    Download the Updated SQL Server 2005 Books Online.

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

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