请各位达人帮我解决下下面这个问题,谢谢下载有这么一个表Table,里面有三个字段Dtime,Name, State,有数据是下面的形式Dtime                      Name  State2008-07-16 10:00:00.000  应用1   1
2008-07-16 10:00:00.000  应用2   0
2008-07-16 10:00:00.000  应用2   1后面两条的Dtime和Name相同
这种结构的数据,可不可以一个sql实现  如果Dtime和Name相同,其中State有为0的,那查出来的结果就只有0那条数据。
换个说法就是如果同一Dtime和Name里有0的数据,就查出0数据来,1那条舍掉。
State是bool行,只能为0,1数据实现结果是这种情况2008-07-16 10:00:00.000 应用1 1
2008-07-16 10:00:00.000 应用2 0
希望各位达人帮我解决,小弟感激不尽

解决方案 »

  1.   

    SELECT DTIME,[NAME],MIN(STATE)
    FROM [TABLE]
    GROUP BY DTIME,[NAME]
      

  2.   


    谢谢这位帅哥,只是我那是bit行,不能直接用MIN
      

  3.   


    因为是bit型,需先转换成int行
      

  4.   

    select * from 表Table 
    where State=0 
    or (State=1 and not exists(select 1 from 表Table t where t.Dtime=Dtime and t.Name =Name and State=0)
      

  5.   


    DECLARE @Table table(Dtime  datetime,[name] nvarchar(200),state bit )insert into @Table select '2008-07-16 10:00:00.000','应用1',1
    insert into @Table select '2008-07-16 10:00:00.000','应用2',0
    insert into @Table select '2008-07-16 10:00:00.000','应用2',1--select * from @tableselect Dtime,[name],min(case state when 0 then 0 else 1 end) as state from @table group by dtime,[name]