select id,  
num,      
visitdate ,
(select count(*) from 表B b where convert(varchar(8),b.registedate,112 )  =  convert(varchar(8),a.visitdate,112 )  and left(CONVERT(varchar(100), b.registedate, 12),2)=left(CONVERT(varchar(100), a.visitdate, 12),2))
from 表A a

解决方案 »

  1.   

    select A.*, unum=(select count(*) from B where datediff(hh, B.registedate, A.visitdate)=0)
    from A
      

  2.   

    --try 1
    select A.*, unum=(select count(*) from B where datediff(hh, B.registedate, A.visitdate)=0)
    from A--try 2
    select A.*, unum=count(*)
    from A, B
    where datediff(hh, B.registedate, A.visitdate)=0
    group by A.id, A.num, A.visitdate
      

  3.   

    select a.* , unum = (select count(*) from b where convert(varchar(13) , registedate , 120) = convert(varchar(13) , visitdate , 120)) from a
      

  4.   

    SET ROWCOUNT 0
    IF OBJECT_ID('TA')IS NOT NULL DROP TABLE  TA
    GO
    CREATE TABLE TA(id INT,  num  INT,     visitdate  DATETIME)
    INSERT TA SELECT 1  ,  675,    '2008-11-26 11:10:10' 
    INSERT TA SELECT 2 ,   98  ,  '2008-11-26 12:00:10' 
    INSERT TA SELECT 3,    123 ,   '2008-11-26 13:02:10' 
    IF OBJECT_ID('TB')IS NOT NULL DROP TABLE  TB
    GO
    CREATE TABLE TB(id INT,    registedate DATETIME) 
    INSERT TB SELECT 1    ,'2008-11-26 11:00:10' 
    INSERT TB SELECT 2    ,'2008-11-26 11:02:10' 
    INSERT TB SELECT 3    ,'2008-11-26 11:10:10' 
    INSERT TB SELECT 4    ,'2008-11-26 11:12:10' 
    INSERT TB SELECT 5    ,'2008-11-26 11:52:10' 
    INSERT TB SELECT 6    ,'2008-11-26 12:10:10' 
    INSERT TB SELECT 7    ,'2008-11-26 12:26:10' 
    INSERT TB SELECT 8    ,'2008-11-26 12:43:10' 
    INSERT TB SELECT 9    ,'2008-11-26 13:00:10' 
    INSERT TB SELECT 10 , '2008-11-26 13:10:10' 
    INSERT TB SELECT 11,  '2008-11-26 13:40:10' 
    SELECT A.*,unum=(SELECT COUNT(*) FROM TB B WHERE  convert(varchar(13),registedate,120 )  =  convert(varchar(13),visitdate,120 )) FROM  TA A
    /*id          num         visitdate                                              unum        
    ----------- ----------- ------------------------------------------------------ ----------- 
    1           675         2008-11-26 11:10:10.000                                5
    2           98          2008-11-26 12:00:10.000                                3
    3           123         2008-11-26 13:02:10.000                                3*/
      

  5.   

    --> By dobear_0922(小熊) 2008-11-26 15:31:01
    --> 测试数据:[A]
    if object_id('[A]') is not null drop table [A]
    create table [A]([id] int,[num] int,[visitdate] datetime)
    insert [A]
    select 1,675,'2008-11-26 11:10:10' union all
    select 2,98,'2008-11-26 12:00:10' union all
    select 3,123,'2008-11-26 13:02:10'
    --> 测试数据:[B]
    if object_id('[B]') is not null drop table [B]
    create table [B]([id] int,[registedate] datetime)
    insert [B]
    select 1,'2008-11-26 11:00:10' union all
    select 2,'2008-11-26 11:02:10' union all
    select 3,'2008-11-26 11:10:10' union all
    select 4,'2008-11-26 11:12:10' union all
    select 5,'2008-11-26 11:52:10' union all
    select 6,'2008-11-26 12:10:10' union all
    select 7,'2008-11-26 12:26:10' union all
    select 8,'2008-11-26 12:43:10' union all
    select 9,'2008-11-26 13:00:10' union all
    select 10,'2008-11-26 13:10:10' union all
    select 11,'2008-11-26 13:40:10'--try 1
    select A.*, unum=(select count(*) from B where datediff(hh, B.registedate, A.visitdate)=0)
    from A--try 2
    select A.*, unum=count(*)
    from A, B
    where datediff(hh, B.registedate, A.visitdate)=0
    group by A.id, A.num, A.visitdate/*
    id          num         visitdate               unum
    ----------- ----------- ----------------------- -----------
    1           675         2008-11-26 11:10:10.000 5
    2           98          2008-11-26 12:00:10.000 3
    3           123         2008-11-26 13:02:10.000 3(3 行受影响)id          num         visitdate               unum
    ----------- ----------- ----------------------- -----------
    1           675         2008-11-26 11:10:10.000 5
    2           98          2008-11-26 12:00:10.000 3
    3           123         2008-11-26 13:02:10.000 3(3 行受影响)
    */
      

  6.   

    select A.*, unum=(select count(*) from B where datediff(mi, B.registedate, A.visitdate)<=60)
    from A
      

  7.   

    DECLARE @TA TABLE(id INT,  num  INT,    visitdate DATETIME)
    INSERT @TA
    SELECT 1,  675,  '2008-11-26 11:10:10' UNION ALL 
    SELECT 2,  98,  '2008-11-26 12:00:10' UNION ALL 
    SELECT 3,  123,  '2008-11-26 13:02:10'DECLARE @TB TABLE(id INT,    registedate DATETIME)
    INSERT @TB
    SELECT 1,  '2008-11-26 11:00:10' UNION ALL 
    SELECT 2,  '2008-11-26 11:02:10' UNION ALL 
    SELECT 3,  '2008-11-26 11:10:10' UNION ALL 
    SELECT 4,  '2008-11-26 11:12:10' UNION ALL 
    SELECT 5,  '2008-11-26 11:52:10' UNION ALL 
    SELECT 6,  '2008-11-26 12:10:10' UNION ALL 
    SELECT 7,  '2008-11-26 12:26:10' UNION ALL 
    SELECT 8,  '2008-11-26 12:43:10' UNION ALL 
    SELECT 9,  '2008-11-26 13:00:10' UNION ALL 
    SELECT 10,  '2008-11-26 13:10:10' UNION ALL 
    SELECT 11,  '2008-11-26 13:40:10'SELECT A.ID,A.NUM,COUNT(*) AS UNUM
    FROM @TA AS A,@TB AS B 
    WHERE ABS(DATEDIFF(MI,visitdate,registedate))<=60 AND DATEPART(HH,registedate)=DATEPART(HH,visitdate) 
    GROUP BY  A.ID,A.NUM,DATEPART(HH,registedate)
    ORDER BY A.ID
    /*
    ID          NUM         UNUM        
    ----------- ----------- ----------- 
    1           675         5
    2           98          3
    3           123         3
    */
      

  8.   

    create table A(id int, num int,     visitdate datetime)
    insert into a values(1,    675,    '2008-11-26 11:10:10') 
    insert into a values(2,    98 ,    '2008-11-26 12:00:10') 
    insert into a values(3,    123,    '2008-11-26 13:02:10') 
    create table b(id int,   registedate datetime)
    insert into b values(1 ,   '2008-11-26 11:00:10') 
    insert into b values(2 ,   '2008-11-26 11:02:10') 
    insert into b values(3 ,   '2008-11-26 11:10:10') 
    insert into b values(4 ,   '2008-11-26 11:12:10') 
    insert into b values(5 ,   '2008-11-26 11:52:10') 
    insert into b values(6 ,   '2008-11-26 12:10:10') 
    insert into b values(7 ,   '2008-11-26 12:26:10') 
    insert into b values(8 ,   '2008-11-26 12:43:10') 
    insert into b values(9 ,   '2008-11-26 13:00:10') 
    insert into b values(10,   '2008-11-26 13:10:10') 
    insert into b values(11,   '2008-11-26 13:40:10') 
    goselect a.* , unum = (select count(*) from b where convert(varchar(13) , registedate , 120) = convert(varchar(13) , visitdate , 120)) from a
    /*
    id          num         visitdate                                              unum        
    ----------- ----------- ------------------------------------------------------ ----------- 
    1           675         2008-11-26 11:10:10.000                                5
    2           98          2008-11-26 12:00:10.000                                3
    3           123         2008-11-26 13:02:10.000                                3
    */select a.* , unum = (select count(*) from b where datediff(hh, registedate , visitdate) = 0) from a
    /*
    id          num         visitdate                                              unum        
    ----------- ----------- ------------------------------------------------------ ----------- 
    1           675         2008-11-26 11:10:10.000                                5
    2           98          2008-11-26 12:00:10.000                                3
    3           123         2008-11-26 13:02:10.000                                3
    */drop table a,b
      

  9.   

    create table A(id int, num int,     visitdate datetime)
    insert into a values(1,    675,    '2008-11-26 11:10:10') 
    insert into a values(2,    98 ,    '2008-11-26 12:00:10') 
    insert into a values(3,    123,    '2008-11-26 13:02:10') 
    create table b(id int,   registedate datetime)
    insert into b values(1 ,   '2008-11-26 11:00:10') 
    insert into b values(2 ,   '2008-11-26 11:02:10') 
    insert into b values(3 ,   '2008-11-26 11:10:10') 
    insert into b values(4 ,   '2008-11-26 11:12:10') 
    insert into b values(5 ,   '2008-11-26 11:52:10') 
    insert into b values(6 ,   '2008-11-26 12:10:10') 
    insert into b values(7 ,   '2008-11-26 12:26:10') 
    insert into b values(8 ,   '2008-11-26 12:43:10') 
    insert into b values(9 ,   '2008-11-26 13:00:10') 
    insert into b values(10,   '2008-11-26 13:10:10') 
    insert into b values(11,   '2008-11-26 13:40:10') 
    goselect a.* , unum = isnull((select count(*) from b where convert(varchar(13) , registedate , 120) = convert(varchar(13) , visitdate , 120)),0) from a
    /*
    id          num         visitdate                                              unum        
    ----------- ----------- ------------------------------------------------------ ----------- 
    1           675         2008-11-26 11:10:10.000                                5
    2           98          2008-11-26 12:00:10.000                                3
    3           123         2008-11-26 13:02:10.000                                3
    */select a.* , unum = isnull((select count(*) from b where datediff(hh, registedate , visitdate) = 0),0) from a
    /*
    id          num         visitdate                                              unum        
    ----------- ----------- ------------------------------------------------------ ----------- 
    1           675         2008-11-26 11:10:10.000                                5
    2           98          2008-11-26 12:00:10.000                                3
    3           123         2008-11-26 13:02:10.000                                3
    */drop table a,b
      

  10.   

    你那两表是如何连接,注册时间与VISITDATE是如何关联的。是datapart(hh,visitdate)吗?