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

怎么查出有丢失的数据,急急急!!
大家好
问一个SQL的问题了我有一个表名为:table里面有以下数据:
peono         data                                     count
001             2007-01-01   08:00:10           1
001             2007-01-01   08:10:10           2
001             2007-01-01   08:20:10           3
001             2007-01-01   08:30:10           4
002             2007-01-01   08:35:10           1
001             2007-01-01   08:40:10           5
002             2007-01-01   08:45:10           2
002             2007-01-01   08:47:10           3  
                                                                    002       2007-01-01   08:48:10       4   (此行丢失)
002             2007-01-01   08:55:10           5
....................
怎么查出在2007-01-01----2007-01-03   这段时间内有丢失count的工号
(出现1     3没有count为2的那一条则算丢失2,
    出现3     5没有count为4的那一条则算丢失4)
我想要得出的结果:在以上table查询
peono
002
怎么查出啊?请帮忙,急急!!!!!!!!

------解决方案--------------------
请参考

编号 姓名
1 张山
3 历史
4 王无
6 李强
桥三
大兵

我想对没有编号的进行编号的自动处理,但是已经有编号的不要进行修改。
如果发现有断号的就将号码分配给没有编号的。
结果如下:
编号 姓名
1 张山
3 历史
4 王无
6 李强
2 桥三
5 大兵


遍历记录,如果id为空,则找出最小未使用id,然后update

找最小未使用id如下:
select (case when exists (select 1 from test where id=1)
then min(id+1) else 1 end)
from test
where id not in(select id-1 from test)


CREATE TABLE [bhb] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[bh] [int] NULL
) ON [PRIMARY]

Declare @intI int
Set @intI = 1
Declare @intChdI int
While @intI < 500 --根据实际需要来确定循环次数
Begin
if not EXISTS(select bh from twhere bh=@intI)
begin
Insert Into bhb(bh) Values (@intI)
end
Set @intI= @intI + 1
End
*/

Declare @intI int
set @intI=1
update t set bh=(select bh from bhb where id=@intI),@intI=@intI+1 where bh is null

drop table bhb


while exists (select 1 from tablename where 编号 is null)
begin
set rowcount 1
update tablename
set 编号=(select min(编号)+1 from tablename a where 编号 is not null
and not exists (select 1 from tablename where 编号=a.编号+1)
)
where 编号 is null
set rowcount 0
end

--检查结果
select * from tablename
order by 编号


---------------------------
题目:
现在数据库由于需要要自己定义唯一编号,唯一编号由3个数字组成。这三个数字我存储为nvarchar.
由于唯一编号在使用中不可能超过999条记录,所以3个足够用。

但是碰到一个问题.第一次使用时添加记录可以001,002,003...这样顺序添加.
当对这些记录进行删除之后再次