我有以下几个字段物品名称,发送部门,接受部门,送出时间。
物品在不挺的流转,也就有很多记录,部门直接都在交接,我想检查针对一个物品前后两条记录接受部门与发送部门不一至的情况有那些,该怎么查询物品       发送部门        接受部门        送出时间
3号操作卡  A部门           B部门            2005-01-01
3号操作卡  B部门           C部门            2005-01-02
3号操作卡  C部门           D部门            2005-01-04
3号操作卡  D部门           E部门            2005-01-06
3号操作卡  C部门           A部门            2005-01-07
这里想检查出这个循环并非正常的。
不知道能做吗?

解决方案 »

  1.   

    create table test(物品 nvarchar(50),发送部门 nvarchar(50),接受部门 nvarchar(50),送出时间 datetime)
    insert into test(物品,发送部门,接受部门,送出时间)
    select '3号操作卡','A部门','B部门','2005-01-01' union all
    select '3号操作卡','B部门','C部门','2005-01-02' union all
    select '3号操作卡','C部门','D部门','2005-01-04' union all
    select '3号操作卡','D部门','E部门','2005-01-06' union all
    select '3号操作卡','C部门','A部门','2005-01-07'select top 2 * from test where 送出时间 >=
    (select 送出时间 from test a 
    where 接受部门 <> (select top 1 发送部门 from test where test.送出时间 > a.送出时间) and 物品 = '3号操作卡')
    and 物品 = '3号操作卡'drop table test
      

  2.   

    我们单位为了检查看谁违反规定流转卡!
    ping3000(Study All Day And All Night) 
    如果我不知道物品名称呢?我们单位有上千张卡,这个一个卡一个卡过来有点难度。
      

  3.   

    试一下:
    create table test(物品 nvarchar(50),发送部门 nvarchar(50),接受部门 nvarchar(50),送出时间 datetime)
    insert into test(物品,发送部门,接受部门,送出时间)
    select '3号操作卡','A部门','B部门','2005-01-01' union all
    select '3号操作卡','B部门','C部门','2005-01-02' union all
    select '3号操作卡','C部门','D部门','2005-01-04' union all
    select '3号操作卡','D部门','E部门','2005-01-06' union all
    select '3号操作卡','C部门','A部门','2005-01-07' union all
    select '1号操作卡','A部门','B部门','2005-01-01' union all
    select '1号操作卡','B部门','C部门','2005-01-02' union all
    select '1号操作卡','C部门','D部门','2005-01-04' union all
    select '1号操作卡','D部门','E部门','2005-01-06' union all
    select '1号操作卡','C部门','A部门','2005-01-07' union all
    select '2号操作卡','A部门','B部门','2005-01-01' union all
    select '2号操作卡','B部门','C部门','2005-01-02' union all
    select '2号操作卡','C部门','D部门','2005-01-04' union all
    select '2号操作卡','D部门','E部门','2005-01-06' union all
    select '2号操作卡','E部门','A部门','2005-01-07' create table #t(id int identity(1,1),物品 nvarchar(50),发送部门 nvarchar(50),接受部门 nvarchar(50),送出时间 datetime)
    insert into #t(物品,发送部门,接受部门,送出时间)
    select 物品,发送部门,接受部门,送出时间 from testselect 物品,发送部门,接受部门,送出时间 from #t where id in(
    select distinct id 
    from #t,test
    where 
    #t.接受部门 <> (select top 1 发送部门 from test where test.送出时间 > #t.送出时间 and #t.物品 = test.物品))drop table test
    drop table #t
      

  4.   

    declare @t table(wp varchar(20) , send_dept varchar(10) , get_dept varchar(10) ,send_time datetime)
    insert into @T/*写入数据*/
    select '3号操作卡',  'A部门',           'B部门',            '2005-01-01' union all
    select ....
    select * from @t
    declare @send_dept varchar(8000)
    declare @send_dept_first varchar(8000)
    declare @send_dept1 varchar(8000)
    declare @get_dept varchar(8000)
    declare @get_dept_first varchar(8000)
    declare @get_dept1 varchar(8000)
    declare cursor1 scroll cursor for select send_dept,get_dept from @t order by send_time
    open cursor1fetch next from cursor1 into @send_dept, @get_dept
    select @send_dept_first = @send_dept
    select @get_dept_first  = @get_dept
    print @get_dept
    print @send_dept
    fetch next from cursor1 into @send_dept1 , @get_dept1
    if(@@fetch_status <> 0)
       begin
          if(@send_dept <> @get_dept)  
             raiserror('error1',16,1)
             goto t1
       end       while @@fetch_status = 0
      begin 
         
        if( @send_dept1 <> @get_dept) 
             begin 
               raiserror('error2',16,1)
               goto T1
           end
        select @send_dept = @send_dept1
        select @get_dept  = @get_dept1
        fetch next from cursor1 into @send_dept1 , @get_dept1
       end if(@send_dept_first = @get_dept) 
      begin 
          print 'success!!'
      end
    else
       begin 
          raiserror('error3',16,1)
       end 
    T1:
    close cursor1
    deallocate cursor1
      

  5.   

    create table tb (物品 varchar(30),       发送部门  varchar(20),      接受部门  varchar(20),      送出时间 datetime)insert into tb values('3号操作卡',  'A部门',           'B部门',            '2005-01-01')
    insert into tb values('3号操作卡',  'B部门',           'C部门',            '2005-01-02')
    insert into tb values('3号操作卡',  'C部门',           'D部门',            '2005-01-04')
    insert into tb values('3号操作卡',  'D部门',           'E部门',            '2005-01-06')
    insert into tb values('3号操作卡',  '3号操作卡',           'A部门',            '2005-01-07')
    select   *from tb a 
    where a.发送部门 <> (select top 1 接受部门 from tb b where a.送出时间>b.送出时间 order by 送出时间 desc )  and  物品='3号操作卡'
      

  6.   

    create table tb (物品 varchar(30),       发送部门  varchar(20),      接受部门  varchar(20),      送出时间 datetime)insert into tb values('3号操作卡',  'A部门',           'B部门',            '2005-01-01')
    insert into tb values('3号操作卡',  'B部门',           'C部门',            '2005-01-02')
    insert into tb values('3号操作卡',  'C部门',           'D部门',            '2005-01-04')
    insert into tb values('3号操作卡',  'D部门',           'E部门',            '2005-01-06')
    insert into tb values('3号操作卡',  '3号操作卡',           'A部门',            '2005-01-07')
    select   *
    from tb a 
    where a.发送部门 <> (select top 1 接受部门 from tb b where a.送出时间>b.送出时间  order by 送出时间 desc ) 
            and (select count(*) from tb where  物品='3号操作卡')<>1 and 物品='3号操作卡'
      

  7.   

    假设表中设一个主键IDselect DISTINCT a.id,a.wupin,a.fasong,a.jieshou,a.times from tb as a INNER JOIN tb AS b ON a.wupin=b.wupinwhere  a.id in(select id from tb where a.fasong <> (select top 1 jieshou from tb  where a.times > times order by times desc))
      

  8.   

    假设表中设一个主键idselect DISTINCT a.id,a.wupin,a.fasong,a.jieshou,a.times from tb as a INNER JOIN tb AS b ON a.wupin=b.wupinwhere  a.id in(select id from tb where a.fasong <> (select top 1 jieshou from tb  where a.times > times order by times desc))