问题提得不好,现在给出:在一天内在同一台消费机器上消费多次(有二次或二次以上消费)的用户。select username ,ndevice,vonvert(varchar(10),ttime,120) as 日期
from T
group by username,ndevice,convert(varchar(10),ttime,120)
having coung(*)>1

解决方案 »

  1.   

    select time,ndevice,from (select idd,dptname, username,left(ttime,10) as time ndrvice,ngroup from T) having coung(*)>1
      

  2.   

    victorycyz(中海,干活去,别在CSDN玩耍!) :
       现在客户想要知道那些人在那些机器上什么时候多次消费了??
      

  3.   

    select username count(ndevice) 
    from T  
    group by username 
    having count(ndevice)>=2
    这样写可以吗?请执教~~~~~~~~~~~~~~!
      

  4.   

    select username ,date(tTime) ,count(ndevice) 
    from T  
    group by username date(tTime) 
    having count(ndevice)>=2 
      

  5.   


    --表结构
    --(ID号,部门ID,用户ID,刷卡时间,设备ID)
    --(tID,userID,userTime,deviceid)--测试数据(组一)
    --(1,'0001','2001-10-10 12:10:00',1)     
    --(2,'0001','2001-10-10 12:10:00',1)    
    --(3,'0001','2001-10-10 12:11:00',1)
    --(4,'0001','2001-10-10 12:11:00',1)    
    --(5,'0001','2001-10-10 12:12:00',1)     
    --(6,'0001','2001-10-10 12:13:00',1)
    --(7,'0001','2001-10-10 12:15:00',1)
    --(8,'0001','2001-10-10 12:15:00',1)--=>目标结果 tID = (1,5,7)
    --测试过程
    declare @T1 table(tID int, userID varchar(4),userTime datetime,deviceid int)
    insert into @T1
           SELECT 1,'0001','2001-10-10 12:10:00',1     
        UNION ALL SELECT 2,'0001','2001-10-10 12:10:00',1    
        UNION ALL SELECT 3,'0001','2001-10-10 12:11:00',1
        UNION ALL SELECT 4,'0001','2001-10-10 12:11:00',1    
        UNION ALL SELECT 5,'0001','2001-10-10 12:12:00',1     
        UNION ALL SELECT 6,'0001','2001-10-10 12:13:00',1
        UNION ALL SELECT 7,'0001','2001-10-10 12:15:00',1
        UNION ALL SELECT 8,'0001','2001-10-10 12:15:00',1--开始查询
    SELECT * FROM @T1 a
    WHERE EXISTS 
    (
    SELECT 1 FROM @t1
        WHERE tid > a.tid AND
             deviceid = a.deviceid AND
             userID = a.userID AND
             abs(DATEDIFF(ss,usertime,a.usertime))<=60
    )--实际查询结果
    /*(所影响的行数为 8 行)tID         userID userTime                                               deviceid    
    ----------- ------ ------------------------------------------------------ ----------- 
    1           0001   2001-10-10 12:10:00.000                                1
    2           0001   2001-10-10 12:10:00.000                                1
    3           0001   2001-10-10 12:11:00.000                                1
    4           0001   2001-10-10 12:11:00.000                                1
    5           0001   2001-10-10 12:12:00.000                                1
    7           0001   2001-10-10 12:15:00.000                                1(所影响的行数为 6 行)
    */
      

  6.   

    SELECT * FROM @T1 a
    WHERE EXISTS 
    (
    SELECT 1 FROM @t1
            WHERE tid < a.tid AND
             deviceid = a.deviceid AND
             userID = a.userID AND
             abs(DATEDIFF(ss,usertime,a.usertime))<=60
    )把以下记录删了,正确吗?
    tID         userID userTime                                               deviceid    
    ----------- ------ ------------------------------------------------------ ----------- 
    2           0001   2001-10-10 12:10:00.000                                1
    3           0001   2001-10-10 12:11:00.000                                1
    4           0001   2001-10-10 12:11:00.000                                1
    5           0001   2001-10-10 12:12:00.000                                1
    6           0001   2001-10-10 12:13:00.000                                1
    8           0001   2001-10-10 12:15:00.000                                1