select  distinct CardNo,date,time, 
   sb_time1,xb_time1
   from T1 我想得到 CardNo,date,time 这三个字段不能有同样的值,同时time <12 值内都不能有重复如:00001  2009-06-01   09:00
    00001  2009-06-01   09:10
    00001  2009-06-01   18:00
    00001  2009-06-02   09:00
    00001  2009-06-02   09:10
    00001  2009-06-02   18:00
我要得到的查询结果是如下:(也就是去得到时间<12点,同时是最早的一个上班时间)    00001  2009-06-01   09:00
 
    00001  2009-06-01   18:00
    00001  2009-06-02   09:00
    
    00001  2009-06-02   18:00

解决方案 »

  1.   

    CardNo,date,time,sb_time1,xb_time1
    from T1 t
    where time=(select min(time) from T1 where CardNo=t.CardNo)
      

  2.   

    select  distinct CardNo,date,time,
      sb_time1,xb_time1
      from T1 where...
      

  3.   

    select * from tb t
    where not exists(select 1 from tb where cardno=t.cardno and time<t.time)
      

  4.   

    select * 
    from tb t 
    where not exists(select 1 from tb where cardno=t.cardno and t.date=date  and time <t.time)
      

  5.   


    ---------------------------------
    --  Author: htl258(Tony)
    --  Date  : 2009-08-20 10:50:27
    ---------------------------------
    --> 生成测试数据表:tbIf not object_id('[tb]') is null
    Drop table [tb]
    Go
    Create table [tb]([CardNo] int,[date] nvarchar(10),[time] nvarchar(5))
    Insert tb
    Select 00001,'2009-06-01','09:00' union all
    Select 00001,'2009-06-01','09:10' union all
    Select 00001,'2009-06-01','18:00' union all
    Select 00001,'2009-06-02','09:00' union all
    Select 00001,'2009-06-02','09:10' union all
    Select 00001,'2009-06-02','18:00'
    Go
    --Select * from tb-->SQL查询如下:
    select *
    from(
    select * from tb t where time=(select MIN(time) from tb where [CardNo]=t.[CardNo] and [date]=t.[date] and time<='12:00')
    union all
    select * from tb t where time=(select MIN(time) from tb where [CardNo]=t.[CardNo] and [date]=t.[date] and time>'12:00')
    ) as t
    order by 1,2,3
    /*
    CardNo      date       time
    ----------- ---------- -----
    1           2009-06-01 09:00
    1           2009-06-01 18:00
    1           2009-06-02 09:00
    1           2009-06-02 18:00(4 行受影响)
    */
      

  6.   

    select
        * 
    from 
       tb t 
    where 
       not exists(select 1 from tb where cardno=t.cardno and time <t.time)
    and
       datepart(hh,[time])<12
      

  7.   

    SELECT * FROM TB T WHERE NOT EXISTS(SELECT 1 FROM TB WHERE CardNo=TCardNo AND date=T.date AND TIME<T.TIME) AND TIME<'12:00'UNION ALLSELECT * FROM TB T WHERE NOT EXISTS(SELECT 1 FROM TB WHERE CardNo=TCardNo AND date=T.date AND TIME>T.TIME) AND TIME>='12:00'
      

  8.   

    select 
    *
    from
    (select
        * 
    from 
       tb t 
    where 
       not exists(select 1 from tb where cardno=t.cardno and time <t.time)
    and
       datepart(hh,[time])<=12
    union all
    select
        * 
    from 
       tb t 
    where 
       not exists(select 1 from tb where cardno=t.cardno and time <t.time)
    and
       datepart(hh,[time])>12) t
    order by 1,2,3
      

  9.   

    create table tb (CardNo varchar(10),date datetime,[time] varchar(10) )
    insert into tb select '00001',  '2009-06-01',  '09:00' 
    union all select     '00001',  '2009-06-01' , '09:10' 
    union all select     '00001',  '2009-06-01' , '09:20'
    union all select     '00001',  '2009-06-01',  '13:00' 
    union all select     '00001',  '2009-06-01',  '15:00' 
    union all select     '00001',  '2009-06-01',  '18:00' 
    union all select     '00001',  '2009-06-02',  '09:00' 
    union all select     '00001',  '2009-06-02',  '09:10' 
    union all select     '00001',  '2009-06-02',  '18:00' select * from tb a 
    where not exists (select 1 from tb where [time]<a.[time]) 
     or left([time],2)>12
    /*
    CardNo     date                    time
    ---------- ----------------------- ----------
    00001      2009-06-01 00:00:00.000 09:00
    00001      2009-06-01 00:00:00.000 13:00
    00001      2009-06-01 00:00:00.000 15:00
    00001      2009-06-01 00:00:00.000 18:00
    00001      2009-06-02 00:00:00.000 09:00
    00001      2009-06-02 00:00:00.000 18:00(6 行受影响)
    */
      

  10.   

    类似这样的语名不对吧?
    select * 
    from tb t 
    where not exists(select 1 from tb where cardno=t.cardno and t.date=date  and time <t.time)那如上的语句 00001  2009-06-01  09:00 
                00001  2009-06-01  09:10  
    这两条记录都出不来了,
    我要得到是记录 00001  2009-06-01  09:00