我有想要成为SQL命令的代码。
我的表leavetabletest
包含以下列:
Holiday, IsRestDay, mNP1, mNP2, mNP3, mNP4, mNP5
首先,我需要分组然后再做案例,如果
怎么做?
Case Holiday='S'
If IsRestDay
mNP4_F8=mNP4_F8+Night_F8
Else
mNP2_F8=mNP2_F8+Night_F8
Endif
Case Holiday='L'
IF IsRestDay
mNP5_F8=mNP5_F8+Night_F8
Else
mNP3_F8=mNP3_F8+Night_F8
Endif
Other
If IsRestDay
mNP2_F8=mNP2_F8+Night_F8
Else
mNP1_F8=mNP1_F8+Night_F8
Endif
我尝试使用案例时,但它不起作用
Select
sum(regular),
case when holiday = 'S' and IsRestDay ='1' then sum(over_F8) end as mOT6_F8
case when holiday = 'S' and IsRestDay ='0' then sum(over_F8) end as mOT3_F8
case when holiday = 'L' and IsRestDay ='1' then sum(over_F8) end as mOT5_F8
case when holiday = 'L' and IsRestDay ='0' then sum(over_F8) end as mOT4_F8
case when holiday <> 'S' and holiday <> 'L' and IsRestDay ='1' then sum(over_F8) end as mOT2_F8
case when holiday <> 'S' and holiday <> 'L' and IsRestDay ='0' then sum(over_F8) end as mOT1_F8
From
leavetabletest
group by
EmployeeID, DepartmentID, SectionID
答案 0 :(得分:1)
如果没有您的数据样本很难给您一个完整的答案,但根据您的尝试,您需要:
holiday
和IsRestDay
添加到您的论坛
EmployeeID
,DepartmentID
和SectionID
添加到您选择的部分(如果这是您需要的 - 不清楚问题)。看起来应该是这样的:
Select EmployeeID
, DepartmentID
, SectionID
, sum(regular)
, case when holiday = 'S' and IsRestDay ='1' then sum(over_F8) end as mOT6_F8
, case when holiday = 'S' and IsRestDay ='0' then sum(over_F8) end as mOT3_F8
, case when holiday = 'L' and IsRestDay ='1' then sum(over_F8) end as mOT5_F8
, case when holiday = 'L' and IsRestDay ='0' then sum(over_F8) end as mOT4_F8
, case when holiday <> 'S' and holiday <> 'L' and IsRestDay ='1' then sum(over_F8) end as mOT2_F8
, case when holiday <> 'S' and holiday <> 'L' and IsRestDay ='0' then sum(over_F8) end as mOT1_F8
From leavetabletest
group by EmployeeID
, DepartmentID
, SectionID
, holiday
, IsRestDay;