sql 连续3年

将日期转换成年  比如2019-10-11(2019) - 2018-10-11(2018)
select a.lx,a.rq
 from tb_da_hj a
 left join tb_da_hj b on a.emid=b.emid and datediff(mm,a.rq+ '-01',getdate())/12>datediff(mm,b.rq+ '-01',getdate())/12
 group by a.lx,a.rq
 having count(distinct case when datediff(mm,a.rq+ '-01',getdate())/12-datediff(mm,b.rq+ '-01',getdate())/12=1 then a.lx end)>0
 and count(distinct case when datediff(mm,a.rq+ '-01',getdate())/12-datediff(mm,b.rq+ '-01',getdate())/12=2 then a.lx end)>0


select distinct CONVERT(varchar(4), rq, 120 ) from tb_da_hj
发布了180 篇原创文章 · 获赞 27 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/sinat_28238111/article/details/104200620
今日推荐