病人的体温每小时量一次病人  时间  温度
甲    0:00  37.3
甲    1:00  37.2
甲    2:00  37.1..     ...    ...
甲    23:00  37.9..     ...    ...
乙    1:00  37.2
 
..     ...    ...丁     ...    ...
..     ...    ...
求 每个病人 温度连续超过37.4 的最长时段比如  甲的时段 是 14:00-20:00丁的时段 是 1:00-2:00 等等 温度是随机变化的,没有规律。如果只有一个小时(不连续)超过37.4 ,就不显示。2000系统

解决方案 »

  1.   

    先给个查出所有的符合条件的select a.病人,a.时间 as 开始时间,b.时间 as 结束时间
    from tab a,tab b
    where a.病人 = b.病人 
    and a.温度>=37.4
    and b.温度>=37.4
    and a.时间 < b.时间
    and not exists (
       select 1
       from tab
       where 病人 = a.病人
        and 温度<37.4
        and 时间 > a.时间
        and 时间 < b.时间
        )
      

  2.   

    有点问题,条件不够select a.病人,a.时间 as 开始时间,b.时间 as 结束时间
    from tab a,tab b
    where a.病人 = b.病人 
    and a.温度>=37.4
    and b.温度>=37.4
    and a.时间 < b.时间
    and not exists (
       select 1
       from tab
       where 病人 = a.病人
        and 温度<37.4
        and 时间 > a.时间
        and 时间 < b.时间
        )
    and isnull((
       select top 1 温度
       from tab 
       where 病人 = a.病人
        and 时间 < a.时间
        order by 时间 desc
       ),0)<37.4
    and isnull((
       select top 1 温度
       from tab 
       where 病人 = b.病人
        and 时间 > b.时间
        order by 时间
       ),0)<37.4
      

  3.   


    create table #tb
    (病人 nvarchar(10),时间 nvarchar(10) ,温度 float)
    insert #tb
    select '甲', '22:00', 37.0 union all
    select '甲', '21:00', 37.8 union all
    select '甲', '20:00', 37.6 union all
    select '甲', '19:00', 37.9 union all
    select '甲', '0:00', 37.7 union all
    select '甲', '1:00', 37.8 union all
    select '甲', '2:00', 37.6 union all
    select '甲', '23:00', 36.9 union all
    select 'A', '0:00', 37.3 union all
    select 'A', '1:00', 37.2 union all
    select 'A', '2:00', 37.4 union all
    select 'A', '23:00', 37.5 union all
    select 'B', '23:00', 36.9 union all
    select 'B', '0:00', 36.3 union all
    select 'B', '1:00', 37.4 union all
    select 'B', '2:00', 37.6 union all
    insert #tb
    select '乙', '1:00', 37.6 union all
    select '乙', '0:00', 37.5 union all
    select '乙', '23:00', 37.7 union all
    select '乙', '2:00', 37.7 union all
    select '乙', '22:00', 37.7select identity(int,1,1) as num,* into #T from #tb where 温度>=37.4 order by 病人,cast(replace(时间,':00','') as int)
    select cast(replace(时间,':00','') as int)-num as ggid,* into #T1 from #T order by num
    select 病人,min(时间) as minTime,max(时间) as maxTime into #T2 from #T1 group by 病人,ggid select t1.病人,
          t1.minTime+'-'+isnull(t2.maxTime,t1.maxTime) as 时间段
    from #T2 as t1 
    left join #T2 as t2 
    on t1.病人=t2.病人 
    and t1.maxTime='23:00' 
    and t2.minTime='0:00'
    where not (t1.minTime='0:00' and exists(select 1 from #T2 as t where t.病人=t1.病人 and t.maxTime='23:00'))
          and (t1.minTime<>t1.maxTime)
    drop table #T
    drop table #T1
    drop table #T2