id      state          time 
1       在线             2012-12-04 02:02-02
2       不在线           2012-12-04 04:02-02
3       不在线           2012-12-04 08:02-02
4       不在线           2012-12-04 10:02-02
5       不在线           2012-12-04 12:02-02
6       不在线           2012-12-04 15:02-02如上,每天返回6条记录,其中一条在线,刚记录这一天为在线状态,只有都为不在线时,这一天记录为不在线状态,现在要查询这台设备一个内的在线状态,请各位帮忙解决下

解决方案 »

  1.   

    select CAST(time as DATE) as Fdate,case when MIN(len(state))=2 then '在线' else '不在线' end state
    from TB
    group by CAST(time as DATE)
      

  2.   

    ----------------------------------------------------------------
    -- Author  :TravyLee(物是人非事事休,欲语泪先流!)
    -- Date    :2012-12-05 10:57:19
    -- Version:--      Microsoft SQL Server 2012 - 11.0.2100.60 (Intel X86) -- Feb 10 2012 19:13:17 -- Copyright (c) Microsoft Corporation-- Enterprise Edition: Core-based Licensing on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)--
    ----------------------------------------------------------------
    --> 测试数据:[test]
    if object_id('[test]') is not null 
    drop table [test]
    go 
    create table [test]
    (
    [id] int,
    [state] varchar(6),
    [time] datetime
    )
    insert [test]
    select 1,'在线','2012-12-04 02:02:02' union all
    select 2,'不在线','2012-12-04 04:02:02' union all
    select 3,'不在线','2012-12-04 08:02:02' union all
    select 4,'不在线','2012-12-05 10:02:02' union all
    select 5,'不在线','2012-12-05 12:02:02' union all
    select 6,'不在线','2012-12-05 15:02:02'
    go
    with t
    as(
    select
    convert(varchar(10),[time],120) as [time],
    sum(case [state] when '在线' then 1 else 0 end) as 在线,
    sum(case [state] when '不在线' then 1 else 0 end) as 不在线,
    count(1) as counts
    from
    test
    group by
    convert(varchar(10),[time],120)
    )
    select 
    [time],
    case when  在线>0 then '今日状态为在线' else '今日状态为不在线' end as 今日状态
    from
    t
    /*
    time 今日状态
    -------------------------------------
    2012-12-04 今日状态为在线
    2012-12-05 今日状态为不在线
    */
      

  3.   


    USE test
    go---->生成表tb
    --if object_id('tb') is not null 
    -- drop table tb
    --Go
    --Create table tb([id] smallint,[state] nvarchar(3),[time] datetime)
    --Insert into tb
    --Select 1,N'在线','2012-12-04 02:02:02'
    --Union all Select 2,N'不在线','2012-12-04 04:02:02'
    --Union all Select 3,N'不在线','2012-12-04 08:02:02'
    --Union all Select 4,N'不在线','2012-12-04 10:02:02'
    --Union all Select 5,N'不在线','2012-12-04 12:02:02'
    --Union all Select 6,N'不在线','2012-12-04 15:02:02'
    --Union all Select 7,N'不在线','2012-12-05 02:02:02' -- for test
    --Union all Select 8,N'不在线','2012-12-05 04:02:02' -- for test
    --Union all Select 9,N'不在线','2012-12-05 08:02:02' -- for test
    --Union all Select 10,N'不在线','2012-12-05 10:02:02' -- for test
    --Union all Select 11,N'不在线','2012-12-05 12:02:02' -- for test
    --Union all Select 12,N'不在线','2012-12-05 15:02:02' -- for test
    SELECT 
    CONVERT(VARCHAR(10),time,120) AS 日期
    ,CASE WHEN EXISTS(SELECT 1 FROM tb AS x 
    WHERE CONVERT(VARCHAR(10),time,120)=CONVERT(VARCHAR(10),a.time,120)
    AND state=N'在线'
    ) THEN N'在线' 
    ELSE N'不在线'  
    END AS 状态
    FROM tb AS a
    GROUP BY CONVERT(VARCHAR(10),time,120)