一次SQLServer实践记录——先一个表中三个日期的最大者,作为查询条件再查询

如下——

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作为条件来查询

这里需要子查询的运用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章