我有此代码,每个月都要检查一次,以查看该人是否在该月入学,最后要告诉您该人是否已入学整年。 Annual
每月检查一次,以查看案例表达式中是否有1
。问题是我无法让SQL识别Jan
和Feb
Select SSN, FirstName, LastName,
Case (DateEnrolled > '1-1-2019' and DateEnrolled < '1-31-2019' ) then 1 else 0 as [Jan],
Case (DateEnrolled > '2-1-2019' and DateEnrolled < '2-28-2019') then 1 else 0 as [Feb],
...
Case (Jan = 1 AND Feb = 1 AND...) then 1 else 0 as [Annual]
from EmployeePerson
答案 0 :(得分:2)
尝试一下:
with cte as
(
Select SSN, FirstName, LastName,
Case (DateEnrolled > '1-1-2019' and DateEnrolled < '1-31-2019' ) then 1 else 0 end as [Jan],
Case (DateEnrolled > '2-1-2019' and DateEnrolled < '2-28-2019') then 1 else 0 end as [Feb],
...
from EmployeePerson
)
select SSN, FirstName, LastName, [Jan], [Feb]...,[Dec],
Case (Jan = 1 AND Feb = 1 AND...AND [Dec] = 1) then 1 else 0 end as [Annual]
from cte
答案 1 :(得分:0)
试试看! SELECT SSN,FirstName,LastName, 案例(日期注册> '01 -01-2019'和日期注册<'01 -31-2019')然后是'简', 案例(日期注册> '02 -01-2019'和日期注册<'02 -28-2019')然后是'二月' 结束为“年度” 来自EmployeePerson;
我认为这会有所帮助。
答案 2 :(得分:0)
尝试
select SSN, FirstName,
Case when Jan is null then 0 else 1 end as Jan,
Case when Feb is null then 0 else 1 end as Feb,
Case when Mar is null then 0 else 1 end as Mar,
Case when Apr is null then 0 else 1 end as Apr,
Case when May is null then 0 else 1 end as May,
Case when Jun is null then 0 else 1 end as Jun,
Case when Jul is null then 0 else 1 end as Jul,
Case when Aug is null then 0 else 1 end as Aug,
Case when Sep is null then 0 else 1 end as Sep,
Case when Oct is null then 0 else 1 end as Oct,
Case when Nov is null then 0 else 1 end as Nov,
Case when [Dec] is null then 0 else 1 end as [Dec],
Case when ([Jan] is not null AND [Feb] is not null AND [Mar] is not null AND
[Apr] is not null AND [May] is not null AND [Jun] is not null AND
[Jul] is not null AND [Aug] is not null AND [Sep] is not null AND
[Oct] is not null AND [Nov] is not null AND [Dec] is not null) then 1 else 0 end as Annual
from(
Select SSN, FirstName, format(DateEnrolled, 'MMM') Enrolled
from EmployeePerson)aa
pivot (max(Enrolled) for Enrolled in([Jan], [Feb], [Mar], [Apr], [May], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec])) as dtl