Select * from tb as a
where exists(Select * from tb where a1=a.a1 and 
              a.ID<>ID and abs(datediff(day,a2,a.a2))<=2)

解决方案 »

  1.   

    select * from tablename a
    where exists (
    select 1 from tablename
    where a1=a.a1
    and id<>a.id
    and abs(datediff(day,a2,a,a2))<=2
    )
      

  2.   

    DECLARE @tb TABLE([id] int, [a1] int, [a2] datetime)
    INSERT INTO @tb
        SELECT  3114, 69, '2007-05-01 00:00:00.000'
        UNION ALL SELECT 2366, 69, '2007-05-01 00:00:00.000'
        UNION ALL SELECT 2095, 69, '2007-07-01 00:00:00.000'
        UNION ALL SELECT 2944, 81, '2007-04-20 00:00:00.000'
        UNION ALL SELECT 2019, 81, '2007-04-22 00:00:00.000'
        UNION ALL SELECT 3094, 81, '2007-04-23 00:00:00.000'
        UNION ALL SELECT 2210, 81, '2007-06-01 00:00:00.000'SELECT * FROM @tb A WHERE (SELECT COUNT(1) FROM @tb WHERE A1 = A.A1 AND DATEDIFF(DAY, A2, A.A2) BETWEEN -2 AND 2)>1
      

  3.   

    declare @ta table(id int,  a1 int, a2 datetime)
    insert @ta
    select 3114, 69, '2007-05-01 00:00:00.000' union all
    select 2366, 69, '2007-05-01 00:00:00.000' union all
    select 2095, 69, '2007-07-01 00:00:00.000' union all
    select 2944, 81, '2007-04-20 00:00:00.000' union all
    select 2019, 81, '2007-04-22 00:00:00.000' union all
    select 3094, 81, '2007-04-23 00:00:00.000' union all
    select 2210, 81, '2007-06-01 00:00:00.000'select * from @ta a
    where exists(select * from @ta where a1=a.a1 and id<>a.id and datediff(day,a2,a.a2) between 0 and 2 )
    or
    exists(select * from @ta where a1=a.a1 and id<>a.id and datediff(day,a.a2,a2)between 0 and 2)(所影响的行数为 7 行)id          a1          a2                                                     
    ----------- ----------- ------------------------------------------------------ 
    3114        69          2007-05-01 00:00:00.000
    2366        69          2007-05-01 00:00:00.000
    2944        81          2007-04-20 00:00:00.000
    2019        81          2007-04-22 00:00:00.000
    3094        81          2007-04-23 00:00:00.000(所影响的行数为 5 行)
      

  4.   

    我没有表达清楚,重说一次
    id      a1       a2
    3114 69 2007-05-01 00:00:00.000
    2366 69 2007-05-01 00:00:00.000
    2095 69 2007-07-01 00:00:00.000
    2944 81 2007-04-20 00:00:00.000
    2019 81 2007-04-22 00:00:00.000
    3094 81 2007-04-25 00:00:00.000
    2210 81 2007-06-01 00:00:00.000找出a1相同,并且a2的日期在正负2天内相同的记录,并标注出相关的
    3114 69 2007-05-01 00:00:00.000    3114,2366
    2366 69 2007-05-01 00:00:00.000    3114,2366
    2944 81 2007-04-20 00:00:00.000    2944,2019
    2019 81 2007-04-22 00:00:00.000    2944,2019,3094
    3094 81 2007-04-25 00:00:00.000    2019,3094
      

  5.   

    我没有表达清楚,重说一次
    id      a1       a2
    3114 69 2007-05-01 00:00:00.000
    2366 69 2007-05-01 00:00:00.000
    2095 69 2007-07-01 00:00:00.000
    2944 81 2007-04-20 00:00:00.000
    2019 81 2007-04-22 00:00:00.000
    3094 81 2007-04-25 00:00:00.000
    2210 81 2007-06-01 00:00:00.000找出a1相同,并且a2的日期在正负2天内相同的记录,并标注出相关的
    3114 69 2007-05-01 00:00:00.000    3114,2366
    2366 69 2007-05-01 00:00:00.000    3114,2366
    2944 81 2007-04-20 00:00:00.000    2944,2019
    2019 81 2007-04-22 00:00:00.000    2944,2019,3094
    3094 81 2007-04-25 00:00:00.000    2019,3094================================================
    2019,3094对应的日期分别是22号跟25号,不在2天范围内阿!
    那最后一条纪录何解?
      

  6.   

    id      a1       a2
    3114 69 2007-05-01 00:00:00.000
    2366 69 2007-05-01 00:00:00.000
    2095 69 2007-07-01 00:00:00.000
    2944 81 2007-04-20 00:00:00.000
    2019 81 2007-04-22 00:00:00.000
    3094 81 2007-04-24 00:00:00.000
    2210 81 2007-06-01 00:00:00.000找出a1相同,并且a2的日期在正负2天内相同的记录,并标注出相关的
    3114 69 2007-05-01 00:00:00.000    3114,2366
    2366 69 2007-05-01 00:00:00.000    3114,2366
    2944 81 2007-04-20 00:00:00.000    2944,2019
    2019 81 2007-04-22 00:00:00.000    2944,2019,3094
    3094 81 2007-04-24 00:00:00.000    2019,3094
    笔误,改了下日期
      

  7.   

    --建表
    create table tab
    (
    id int,
    a1 int,
    a2 datetime
    )insert into tab select 3114, 69, '2007-05-01 00:00:00.000'
    insert into tab select 2366, 69, '2007-05-01 00:00:00.000'
    insert into tab select 2095, 69, '2007-07-01 00:00:00.000'
    insert into tab select 2944, 81, '2007-04-20 00:00:00.000'
    insert into tab select 2019, 81, '2007-04-22 00:00:00.000'
    insert into tab select 3094, 81, '2007-04-24 00:00:00.000'
    insert into tab select 2210, 81, '2007-06-01 00:00:00.000'--建立函数
    create function f_addstr(@id int)
    returns varchar(2000)
    as
    begin
    declare @str varchar(2000)
    set @str = ''
    select @str = @str + ',' + cast(bid as varchar) from tab2 where id = @id
    return (@str)
    end
    go--导入临时表
    select a.id,a.a1,a.a2,b.id as bid
    into tab2
    from tab a left join tab b on a.a1 = b.a1 and abs(datediff(day,a.a2,b.a2)) <= 2--查询
    select id,a1,a2,dbo.f_addstr(id) as ida 
    from tab2 t1
    where exists (select 1 from tab2 where id = t1.id and bid <> t1.bid)
    group by id,a1,a2
    order by a1,a2--结果
    2366
    69 2007-05-01 00:00:00.000 3114,2366 3114
    69 2007-05-01 00:00:00.000 3114,2366 2944
    81 2007-04-20 00:00:00.000 2944,2019 2019
    81 2007-04-22 00:00:00.000 2944,2019,3094 3094
    81 2007-04-24 00:00:00.000 2019,3094 --删除环境
    drop table tab
    drop table tab2
      

  8.   

    --晕,查询语句没有贴最新的! -_-!!
    --语句
    select id,a1,a2,STUFF(dbo.f_addstr(id),1,1,'') as ida 
    from tab2 t1
    where exists (select 1 from tab2 where id = t1.id and bid <> t1.bid)
    group by id,a1,a2
    order by a1,a2