日期:2014-05-18  浏览次数:20325 次

SQL语句,请帮忙!
表A
employeeno consumemoney balancemoney date count
0001 1 10 2007-10-01 10:05:02 5
0002 3 50 2007-10-01 10:10:23 30
0001 2 8 2007-10-01 15:01:20 6
0001 1 6 2007-10-02 12:13:28 8
0001 2 4 2007-10-05 08:09:43 9
0002 2 48 2007-10-06 19:23:00 31
0002 5 43 2007-10-07 08:20:16 32
0002 3 10 2007-10-08 17:10:40 34
0002 2 8 2007-10-09 08:01:07 35
0001 1 3 2007-10-09 16:38:16 10
......

9999

如何写SQL语句查出count(记录号)缺少/丢失记录号的人员,
如:员工0001丢失记录号7
  员工0002丢失记录号33

结果如下:

----------------
employeeno
0001
0002



------解决方案--------------------
SQL code
create table tb(employeeno varchar(10),consumemoney int, balancemoney int,date datetime,[count] int) 
insert into tb values('0001',1,      10,      '2007-10-01 10:05:02',  5 )
insert into tb values('0002',3,      50,      '2007-10-01 10:10:23',  30)
insert into tb values('0001',2,        8,      '2007-10-01 15:01:20',  6 )
insert into tb values('0001',1,        6,      '2007-10-02 12:13:28',  8 )
insert into tb values('0001',2,        4 ,      '2007-10-05 08:09:43',  9 )
insert into tb values('0002',2,      48,      '2007-10-06 19:23:00',  31)
insert into tb values('0002',5,      43,      '2007-10-07 08:20:16',  32)
insert into tb values('0002',3,      10,      '2007-10-08 17:10:40',  34)
insert into tb values('0002',2,        8,      '2007-10-09 08:01:07',  35)
insert into tb values('0001',1,        3,      '2007-10-09 16:38:16',  10)
go

select m.employeeno,m.[count]+1 缺号 from
(select px=(select count(1) from tb where employeeno=a.employeeno and [count] <a.[count])+1 , * from tb a) m,
(select px=(select count(1) from tb where employeeno=a.employeeno and [count] <a.[count])+1 , * from tb a) n
where m.employeeno = n.employeeno and m.px = n.px - 1 and m.[count] <> n.[count] - 1

drop table tb

/*
employeeno 缺号     
---------- -----------
0001    7
0002    33

(所影响的行数为 2 行)
*/