February 23, 2013 at 11:26 pm
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.
February 24, 2013 at 3:10 am
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.
February 24, 2013 at 10:27 am
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
February 24, 2013 at 10:46 am
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