日期:2014-05-18 浏览次数:20512 次
if object_id('[tb]') is not null drop table [tb] go create table [tb]([工号] varchar(5),[休息日] varchar(5),[次数] int) insert [tb] select '00010','1,5',2 union all select '00013','4,6',2 union all select '00016','4,5,6',2 union all select '00034','1,2',1 union all select '00038','1,6',1 union all select '00042','3,4',2 union all select '00043','4',1 union all select '00049','1,3',2 union all select '00058','4',1 union all select '00060','2',1 union all select '00064','2',1 union all select '00065','2',1 union all select '00067','4,5,6',3 go ;with t1 as( select a.工号,b.wk from tb a join ( select 1 as wk union select 2 union select 3 union select 4 union select 5 union select 6 ) b on charindex(ltrim(wk),休息日)=0 ) select * from t1 t where wk=(select top 1 wk from t1 where 工号=t.工号 order by wk desc) /** 工号 wk ----- ----------- 00010 6 00013 5 00016 3 00034 6 00038 5 00042 6 00043 6 00049 6 00058 6 00060 6 00064 6 00065 6 00067 3 (13 行受影响) **/