SQL Server Performance

Query

Discussion in 'General Developer Questions' started by emamuthu, Nov 4, 2006.

  1. emamuthu New Member

    Hello everyone,
    How do we write a query for something like this

    Quarter month amount
    ------- ---- ------
    1 1 10
    1 2 15
    1 3 12
    2 4 10
    2 5 11
    2 6 12

    when we pass quarter as parameter , i need to display like below

    Month amount
    -----
    1+2 25
    2+3 27

    How do we display for the first quarter

    Thaks
    emamuthu

  2. FrankKalis Moderator

    Consider doing this at the client. In the meantime what about:


    SELECT CAST(MAX(a.month) AS VARCHAR(2)) + '+' + CAST(MIN(b.month) AS VARCHAR(2)) AS Month
    , MAX(a.Amount)+ MIN(b.Amount) AS Amount
    FROM your_table a
    JOIN your_table b
    ON a.quarter = b.quarter
    AND a.month < b.month
    WHERE a.quarter = 1
    GROUP BY b.month


    --
    Frank Kalis
    Moderator
    Microsoft SQL Server MVP
    Webmaster:http://www.insidesql.de
  3. emamuthu New Member

    thanks frank ..that works for me.

    Thanks
    emamuthu

Share This Page