Query Help

  • i have table with following structure.

    SELECT SaleID, vender_inv, Code, cDate

    FROM SaleMain

    i want to fetch last two invoice against each code.

    where saleid is auto incremented and cdate as well to get last invoice.

    help will be really appreciated.

  • You might want to look into the CROSS APPLY syntax (use it to get the Top 2 vender_inv ORDER BY SaleID DESC) together with a cte or subquery to return all distinct code values.



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Something like this should do it:

    --i have table with following structure.

    --SELECT SaleID, vender_inv, Code, cDate

    --FROM SaleMain

    --i want to fetch last two invoice against each code.

    --where saleid is auto incremented and cdate as well to get last --invoice.

    select *

    from

    (

    select SaleId, Vender_Inv, Code, cDate,

    ROW_NUMBER() over (partition by Code order by cDate Desc) as RowNumber

    FROM SaleMain

    ) A

    where A.RowNumber < 3

  • that's awesome laurie thanks a ton 🙂

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

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