如下——
select * from ( select *,(select MAX(v) from (values (convert(date,left(date1,10),23)), (convert(date,left(date2,10),23)), (convert(date,left(date3,10),23)) ) as value(v) ) as latest_date from GZTest2.dbo.ageing where inspector = '張三' ) p where CONVERT(date,latest_date,23) between CONVERT(date,'2021-05-01',23) and CONVERT(date,'2021-06-01',23)
因爲日期格式為2021-04-28的字符串,需要用convert轉化為date類型
取date1 date2 date3三者中最晚的日期
查詢后作爲latest_date
再以latest_date作爲條件來查詢
這裏需要子查詢的運用