1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
select Pub.Code, Pub.Name, WeekdayName = DateName(weekday,Cast(Cast(EditionDate as nvarchar) as datetime)), EditionDate from ( SELECT *, --adds a row number ROW_NUMBER() OVER (PARTITION BY lnkpublicationID ORDER BY EditionDate DESC) AS rn FROM src_PublicationEdition where EditionDate between 20140101 and 20180101 ) a Inner Join src_Publication PUB on PUB.IDPublication = a.lnkPublicationID --we can then limit by row number where rn <= 10 --- OR ;with MAIN As ( select Pub.Code, Pub.Name, WeekdayName = DateName(weekday,Cast(Cast(EditionDate as nvarchar) as datetime)), EditionDate, rn from ( SELECT *, ROW_NUMBER() OVER (PARTITION BY lnkpublicationID ORDER BY EditionDate DESC) AS rn FROM src_PublicationEdition where EditionDate between 20140101 and 20180101 ) a Inner Join src_Publication PUB on PUB.IDPublication = a.lnkPublicationID ) select * from MAIN where rn < 10 order by Code |