坐标表   table1   有以下字段:        uID(人员ID)           thedate(时间)         pointID(位于地点)
记录1:      1              2008-8-7 13:00:00              办公室
记录2:      1              2008-8-7 13:17:00              厂房
记录3:      2              2008-8-7 13:00:00              办公室
记录4:      2              2008-8-7 13:25:00              厂房
记录5:      3              2008-8-7 13:25:10              办公室
记录6:      4              2008-8-7 13:25:11              厂房我需要查询的结果是:
在2008-8-7 13:26:00  在"厂房"的人数有  多少个人

解决方案 »

  1.   


    select count(1) from table1 where pointid = '厂房'
      

  2.   

    o ,sorry ,看错了select count(1) 
    from table1 
    where pointid = '厂房'
    and  thedate = '2008-8-7 13:26:00'
      

  3.   

    select count(1) from table1 where pointid = '厂房'
    and thedate =‘2008-8-7 13:26:00  ’
      

  4.   

    select count(*) from table1 where pointid = '厂房'
      

  5.   

    select count(1) as 人数 from tb1 where pointID='厂房' and thedate='2008-8-7 13:26:00'
      

  6.   

    你们都错了,thedate 也不可能去定义等于那个时间,因为要算的不是等于
    比如记录6:      4              2008-8-7 13:25:11              厂房 
    这个人25分就来到了厂房,26分也应该在厂房,只要没有下一条记录说明他在其他地方
      

  7.   

    select count(*) from (select distinct uID
    from table1 
    where pointid = '厂房'
    and  thedate = '2008-8-7 13:26:00') a 试试,接分
      

  8.   

    select count(*) from (select distinct uID 
    from table1 
    where pointid = '厂房' 
    and  thedate in (select max(thedate) from  table1 )) a 最大时间,OK
      

  9.   

    这个呢?select count(1) from table1 where pointid = '厂房'
    and ((select max(thedate) from table1 where pointid = '厂房')>'2008-8-7 13:26:00 '
    or (select min(thedate) from table1 where pointid = '厂房')<='2008-8-7 13:26:00 ')
      

  10.   

    select count(*) from table1 where pointid = '厂房' and thedate='2008-8-7 13:26:00'这样写满足你的需求,但可能满足不了你的业务,上面这个SQL精确到秒。像上面这种业务一般是精确到天数,小时。下面是精确到某天的写法:
    select count(*) from table1 where pointid = '厂房' and thedate>='2008-8-7 00:00:00' and thedate<='2008-8-7 23:59:59'
      

  11.   

    begin transelect uid,max(thedate) into #table1
    from table1 group by uIDdeclare @max_qty integerselect @max_qty = count(*) from #table1 as a,table1 as b
    where a.uid = b.uid and a.thedate = b.thedate and b.pointid = '厂房'
    select @max_qty --rollback tran
      

  12.   

    select count(1) from table1 where pointid = '厂房'
    and  select min(thedate) from table1 where pointid = '厂房')=< '2008-8-7 13:26:00'
      

  13.   

    经过测试,18楼同仁的最接近,但最后有点问题,就是select count(*) from (select distinct uID 
    from table1 
    where pointid = '厂房' 
    and  thedate in (select max(thedate) from  table1 )) a 但是MAX(thedate)好象是全部记录的MAX,而不是distinct uID那个人的MAX
      

  14.   

    经过测试,18楼同仁的最接近,但最后有点问题,就是 select count(*) from (select distinct uID 
    from table1 
    where pointid = '厂房' 
    and  thedate in (select max(thedate) from  table1 )) a 但是MAX(thedate)好象是全部记录的MAX,而不是distinct uID那个人的MAX的时间
      

  15.   

    经过测试,18楼同仁的最接近,但最后有点问题,就是 select count(*) from (select distinct uID 
    from table1 
    where pointid = '厂房' 
    and  thedate in (select max(thedate) from  table1 )) a 但是MAX(thedate)好象是全部记录的MAX,而不是distinct uID那个人的MAX的时间
      

  16.   

    create table #t(uid int,thedate datetime,pointID varchar(20))
    insert #t
    select        1        ,    '2008-8-7 13:00:00'    ,         '办公室 '
     union all select   1        ,    '2008-8-7 13:17:00'     ,        '厂房'
     union all select   2        ,    '2008-8-7 13:00:00'     ,        '办公室' 
     union all select   2        ,    '2008-8-7 13:25:00'     ,        '厂房' 
     union all select   3        ,    '2008-8-7 13:25:10'     ,        '办公室' 
     union all select   4        ,    '2008-8-7 13:25:11'     ,        '厂房' 
    select count(1) from #t t
    inner join (
    select uid,max(thedate) as thedate1 from #t  group by uid )c
    on c.uid=t.uid and c.thedate1=t.thedatewhere t.pointID='厂房'
      

  17.   

    cyc_cheng 兄弟,为什么用你的例后显示不支持连接表达式
      

  18.   

    select count(uID) from (select distinct uID 
    from table1 a
    where a.pointid = '厂房' 
    and  a.thedate =(select max(thedate) from table1 b where b.uID=a.uID ))  
    试试这样可不可以吧
      

  19.   

    不知道有啥区别,我在sql 2005里面显示都正常
    我测试过基本上上面的应该是正确的