TOP FUNCTION

  • Hi

    I need an urgent query to retrieve top 2 invoices for each account. An account can have 'n' number of invoices. but i need a query which will retrieve top 2 invoices for each account.

    Is there any single query???????

  • Yes.


    N 56°04'39.16"
    E 12°55'05.25"

  • What defines which invoices are considered top for each account? Invoice daet? Amount? Number of items ordered?

    Can you please post the schema of the tables involved, some sample data and what you want the outout to be.

    See Forum Etiquette[/url]

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Example :

    inv acc date

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

    inv1 acc1 10/12/2007

    inv2 acc1 11/12/2007

    inv3 acc1 12/12/2007

    inv4 acc2 11/12/2007

    inv5 acc2 12/12/2007

    inv6 acc3 13/12/2007

    output should be as follows:-

    inv2 acc1 11/12/2007

    inv3 acc1 12/12/2007

    inv5 acc2 12/12/2007

    inv6 acc3 13/12/2007

  • Why would you not like this output?

    inv2 acc1 11/12/2007

    inv3 acc1 12/12/2007

    inv4 acc2 11/12/2007

    inv5 acc2 12/12/2007

    inv6 acc3 13/12/2007


    N 56°04'39.16"
    E 12°55'05.25"

  • i need to select only the latest 2 invoices for that account so inv4 should not be retrieved.

  • I see that you need an "urgent" query. Does that mean this is homework? What have you tried so far?

    John

  • Ananth (12/18/2007)


    i need to select only the latest 2 invoices for that account so inv4 should not be retrieved.

    In the sample data you gave it is one of the latest 2, in fact one of the only two, invoices for acct2

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • YES, I NEED ONLY LATEST 2 INVOICE FOR THAT ACCOUNT....DO YOU HAVE THE QUERY?????

  • i used cursors for the above result.... but i need to avoid the cusrsor????

  • When is the assignment due?

    Don't worry. You will have a suggestion before that.


    N 56°04'39.16"
    E 12°55'05.25"

  • DECLARE@Sample TABLE (Invoice CHAR(4), Account CHAR(4), Date DATETIME)

    SET DATEFORMAT DMY

    INSERT@Sample

    SELECT'inv1', 'acc1', '10/12/2007' UNION ALL

    SELECT'inv2', 'acc1', '11/12/2007' UNION ALL

    SELECT'inv3', 'acc1', '12/12/2007' UNION ALL

    SELECT'inv4', 'acc2', '11/12/2007' UNION ALL

    SELECT'inv5', 'acc2', '12/12/2007' UNION ALL

    SELECT'inv6', 'acc3', '13/12/2007'

    SELECTs.Account,

    MAX(s.Date) AS Date

    FROM@Sample AS S

    WHERENOT EXISTS(

    SELECTw.Account,

    MAX(w.Date)

    FROM@Sample AS w

    GROUP BYw.Account

    HAVINGs.Account = w.Account

    AND s.Date = MAX(w.Date)

    )

    GROUP BYs.Account

    UNION ALL

    SELECTw.Account,

    MAX(w.Date)

    FROM@Sample AS w

    GROUP BYw.Account

    ORDER BYAccount,

    Date DESC


    N 56°04'39.16"
    E 12°55'05.25"

  • Thanks peter!!!!!!!!

    Itz really useful for me. Now the above query will return latest 2 invoices for that account. if i need to return latest 10 or 12 invoices for that account.......what modification should be done in the above query.....

  • -- Prepare sample data

    DECLARE@Sample TABLE (Invoice CHAR(4), Account CHAR(4), Date DATETIME)

    SET DATEFORMAT DMY

    INSERT@Sample

    SELECT'inv1', 'acc1', '10/12/2007' UNION ALL

    SELECT'inv2', 'acc1', '11/12/2007' UNION ALL

    SELECT'inv3', 'acc1', '12/12/2007' UNION ALL

    SELECT'inv4', 'acc2', '11/12/2007' UNION ALL

    SELECT'inv5', 'acc2', '12/12/2007' UNION ALL

    SELECT'inv6', 'acc3', '13/12/2007'

    -- Create staging area

    DECLARE@Stage TABLE (RowID INT IDENTITY, Invoice CHAR(4), Account CHAR(4), Date DATETIME)

    INSERT@Stage

    (

    Invoice,

    Account,

    Date

    )

    SELECTInvoice,

    Account,

    Date

    FROM@Sample

    ORDER BYAccount,

    Date DESC

    DECLARE@NumberOfRecordsPerAccount INT

    SET@NumberOfRecordsPerAccount = 10

    -- Show the expected output

    SELECTs.Invoice,

    s.Account,

    s.Date

    FROM@Stage AS s

    INNER JOIN(

    SELECTMIN(RowID) AS MinRow,

    MIN(RowID) + @NumberOfRecordsPerAccount AS MaxRow,

    Account

    FROM@Stage

    GROUP BYAccount

    ) AS d ON d.Account = s.Account

    WHEREs.RowID >= d.MinRow

    AND s.RowID < d.MaxRow

    ORDER BYs.Account,

    s.Date DESC


    N 56°04'39.16"
    E 12°55'05.25"

  • Thanks Peter!!!

Viewing 15 posts - 1 through 15 (of 20 total)

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