34,594
社区成员
发帖
与我相关
我的任务
分享
year month channel product week qty datatype qtytype date
2018 5 Chain and Dept. Store 02233710 18 0 Display_Inv Actual 2019-02-13 10:49:40.773
2018 5 Chain and Dept. Store 02233710 19 0 Display_Inv Actual 2019-02-13 10:49:40.773
2018 5 Chain and Dept. Store 02233710 21 0 Display_Inv Actual 2019-02-13 10:49:40.773
2018 5 Chain and Dept. Store 02439713 18 0 Display_Inv Actual 2019-02-13 10:49:40.773
2018 5 Chain and Dept. Store 02439713 21 0 Display_Inv Actual 2019-02-13 10:49:40.773
--以上是数据表
--以下是想要的结果
year month channel product week qty datatype qtytype date
2018 5 Chain and Dept. Store 02233710 21 0 Display_Inv Actual 2019-02-13 10:49:40.773
2018 5 Chain and Dept. Store 02439713 21 0 Display_Inv Actual 2019-02-13 10:49:40.773
求一段sql
SELECT *
FROM
(SELECT *
,ROW_NUMBER() OVER (PARTITION BY year, month, channel, product ORDER BY week DESC) AS RN
FROM tab) AS T
WHERE RN=1
select *
from [表名] a
where not exists(select 1
from [表名] b
where b.year=a.year
and b.month=a.month
and b.channel=a.channel
and b.product=a.product
and b.week>a.week)