select item,fdate(完整日期),hours
from #a
想实现:,当fdate<2023,则hours 显示 工时1,
当时fdate>=2023,则hours另起一个字段,显示工时2,
求在一个sql中如何实现?
cas查询里面的hours换成:when fdate<2023 then 1 when fdate>=2023 then 2 end as hours
可以简化为:when fdate<2023 then 1 else 2 end as hours
SELECT a1.item, a1.fdate AS 工时1, a2.fdate AS 工时2
FROM (SELECT item, fdate FROM a WHERE fdate < 2023) a1
JOIN (SELECT item, fdate FROM a WHERE fdate >= 2023) a2
ON a1.item = a2.item
使用case when试试