日期:2014-05-17 浏览次数:20572 次
create table u01
(periodid varchar(10),SFQYBJ decimal(5,2),DCNO varchar(10))
-- 测试1
truncate table u01
insert into u01
select '1310',1.00,'000118' union all
select '1309',0.00,'000118' union all
select '1308',1.00,'000118' union all
select '1307',0.00,'000118' union all
select '1306',0.00,'000118' union all
select '1305',0.00,'000118' union all
select '1304',0.00,'000118' union all
select '1303',0.00,'000118' union all
select '1302',0.00,'000118' union all
select '1301',0.00,'000118'
with t as
(select row_number() over(partition by DCNO order by periodid) 'rn',
SFQYBJ
from u01)
select sum(case when a.SFQYBJ=1 and (b.SFQYBJ=1 or c.SFQYBJ=1) then 1
else 0 end) '全月病假月数'
from t a
left join t b on a.rn=b.rn+1
left join t c on a.rn=c.rn-1
/*
全月病假月数
-----------
0
(1 row(s) affected)
*/
-- 测试2
truncate table u01
insert into u01
select '1310',1.00,'000118' union all
select '1309',1.00,'000118' union all --> 1309对应的SFQYBJ换成1.00
select '1308',1.00,'000118' union all
select '1307',0.00,'000118' union all
select '1306',0.00,'000118' union all
select '1305',0.00,'000118' union all
select '1304',0.00,'000118' union all
select '1303',0.00,'000118' union all