Retrieving data from the same column

  • Hello,

    I have a table Orders

    By applying sql query on it I am getting result set(table) as follows

    sum(freight) | YEAR(orderdate)

    -------------+------------------

    ----------- +------------------

    25000 + 1997

    -------------+------------------

    30000 + 1998

    Now I want to use 1997's sum(freight) as Target and 1998's sum(freight) as Current.My query should be such that it will fetch following resultset.

    Target | Current

    ----------------

    25000 | 30000

    Please can anyone help me with the query?

    Regards,

    Snehalata

  • snehalata_d (8/9/2010)


    Hello,

    I have a table Orders

    By applying sql query on it I am getting result set(table) as follows

    sum(freight) | YEAR(orderdate)

    -------------+------------------

    ----------- +------------------

    25000 + 1997

    -------------+------------------

    30000 + 1998

    Now I want to use 1997's sum(freight) as Target and 1998's sum(freight) as Current.My query should be such that it will fetch following resultset.

    Target | Current

    ----------------

    25000 | 30000

    Please can anyone help me with the query?

    Regards,

    Snehalata

    This is a pivot query, see Pivot in books online, or better still check out this excellent article by Jeff Moden:

    http://www.sqlservercentral.com/articles/T-SQL/63681/[/url]

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • You have to use pivot table

  • Thanks a lot.

    With the help of the useful article ,I was able to solve my problem.:-)

Viewing 4 posts - 1 through 3 (of 3 total)

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