我有以下SQL Server查询
select r.isactive,r.workingyear,r.startperiod,r.endperiod,r.anniversary
from setup_holiday_policy t cross apply
(select data
from dbo.Split(t.scheduleapplication, ',')
) di cross apply
(select max(case when did.id = 1 then did.data end) as isactive,
max(case when did.id = 2 then did.data end) as workingyear,
max(case when did.id = 3 then did.data end) as anniversary,
max(case when did.id = 4 then did.data end) as startperiod,
max(case when did.id = 5 then did.data end) as endperiod
from dbo.Split(di.data,':') did
) r
WHERE r.workingyear = @employeeworkingyears
策略表在workingyear字段中的值可以为0。这意味着当该字段为0时,我应该返回默认记录。
setup_holiday_policy
因此,如果@employeeworkingyears = 2
并且workingyears = 2
中有否 setup_holiday_policy
,我应该返回工作日字段中具有0值的默认行。
这是返回行的示例。
有任何线索如何实现这一目标?
答案 0 :(得分:0)
如果只返回一行(如示例数据所示),您可以使用top
执行此操作:
select top 1 r.isactive,r.workingyear,r.startperiod,r.endperiod,r.anniversary
from setup_holiday_policy t cross apply
(select data
from dbo.Split(t.scheduleapplication, ',')
) di cross apply
(select max(case when did.id = 1 then did.data end) as isactive,
max(case when did.id = 2 then did.data end) as workingyear,
max(case when did.id = 3 then did.data end) as anniversary,
max(case when did.id = 4 then did.data end) as startperiod,
max(case when did.id = 5 then did.data end) as endperiod
from dbo.Split(di.data,':') did
) r left outer join
(select @employeeworkingyears as employeeworkingyears
) e
on
WHERE r.workingyear in (@employeeworkingyears, 0)
order by r.workingyear desc;