几种精妙的断号查询方法
时 间:2016-07-08 08:11:19
作 者:缪炜 ID:24010 城市:江阴
摘 要:断号查询是审计分析的重要手段。在审计过程中,我们经常遇到这样的情况,税务机关开票系统开的税票号存在断号、医院的门诊收费系统开出收据的编号断号、行政事业机关的非税收入管理系统开的收据断号等,这些票据除了正常作废导致断号之外,往往还存在其他审计疑点。因此,准确地定位断号位置、范围,成为审计分析的一个关键点。
正 文:
AO系统中虽然提供了断号分析功能(如上图),但是面对海量数据时,还是直接在数据库里操作比较方便,因此有必要研究一些高效的断号查询算法。下面是我收集的一些定位断号位置、范围的算法,很精妙,很值得研究:
USE tempdb;
GO
IF OBJECT_ID('dbo.T1') IS NOT NULL
Drop TABLE dbo.T1
GO
Create TABLE dbo.T1(col1 INT NOT NULL PRIMARY KEY);
Insert INTO dbo.T1(col1) VALUES(1);
Insert INTO dbo.T1(col1) VALUES(2);
Insert INTO dbo.T1(col1) VALUES(3);
Insert INTO dbo.T1(col1) VALUES(100);
Insert INTO dbo.T1(col1) VALUES(101);
Insert INTO dbo.T1(col1) VALUES(103);
Insert INTO dbo.T1(col1) VALUES(104);
Insert INTO dbo.T1(col1) VALUES(105);
Insert INTO dbo.T1(col1) VALUES(106);
GO
IF OBJECT_ID('dbo.T1') IS NOT NULL
Drop TABLE dbo.T1
GO
Create TABLE dbo.T1(col1 INT NOT NULL PRIMARY KEY);
Insert INTO dbo.T1(col1) VALUES(1);
Insert INTO dbo.T1(col1) VALUES(2);
Insert INTO dbo.T1(col1) VALUES(3);
Insert INTO dbo.T1(col1) VALUES(100);
Insert INTO dbo.T1(col1) VALUES(101);
Insert INTO dbo.T1(col1) VALUES(103);
Insert INTO dbo.T1(col1) VALUES(104);
Insert INTO dbo.T1(col1) VALUES(105);
Insert INTO dbo.T1(col1) VALUES(106);
--定位断号位置、范围的算法
--方法1
Select startnum,
(Select MIN(col1)-1 FROM T1
Where col1>startnum) AS endnum
FROM (Select col1+1 AS startnum FROM T1
Where col1+1 NOT IN (Select col1 FROM T1)
and col1 <(Select max(col1) FROM T1) ) AS maxnum
orDER BY startnum
(Select MIN(col1)-1 FROM T1
Where col1>startnum) AS endnum
FROM (Select col1+1 AS startnum FROM T1
Where col1+1 NOT IN (Select col1 FROM T1)
and col1 <(Select max(col1) FROM T1) ) AS maxnum
orDER BY startnum
--方法2: 用not exists代替not in 后代码更简洁,查询效率更高
select col1+1 as startnum,
(select MIN(col1)-1 from T1 where k.col1<col1) as endnum
from T1 k
where not exists(select * from T1 where k.col1+1=col1)
and col1<(select MAX(col1) from T1) --防止最大数字的后一个数字出现
(select MIN(col1)-1 from T1 where k.col1<col1) as endnum
from T1 k
where not exists(select * from T1 where k.col1+1=col1)
and col1<(select MAX(col1) from T1) --防止最大数字的后一个数字出现
--方法3:利用前后2个数字相减的差来判断是否连续,当差大于1则不连续。此方法可用于判断任意等差序列是否连续。
select CUr+1 as stratnum,nxt-1 as endnum
from(
select col1 as cur,(select MIN(col1) from T1 where k.col1<col1) as nxt
from T1 k) p
where nxt-cur>1
from(
select col1 as cur,(select MIN(col1) from T1 where k.col1<col1) as nxt
from T1 k) p
where nxt-cur>1
--再来看看已有范围的查询
--方法1:利用比当前值大的最接近最小间断值的值
select MIN(col1) as startnum, grp as endnum
from(
select col1,
(select MIN(col1) from T1 a
where a.col1>=k.col1 and
not exists(select * from T1 where a.col1+1=col1 )
) as grp
from T1 k ) z
group by grp
--方法1:利用比当前值大的最接近最小间断值的值
select MIN(col1) as startnum, grp as endnum
from(
select col1,
(select MIN(col1) from T1 a
where a.col1>=k.col1 and
not exists(select * from T1 where a.col1+1=col1 )
) as grp
from T1 k ) z
group by grp
--方法2:利用ROW_NUMBER
select MIN(col1) as startnum,
max(col1) as endnum
from(
select col1,
col1-ROW_NUMBER()over(order by col1) as grp
from T1 k ) z
group by grp
select MIN(col1) as startnum,
max(col1) as endnum
from(
select col1,
col1-ROW_NUMBER()over(order by col1) as grp
from T1 k ) z
group by grp
Access软件网QQ交流群 (群号:54525238) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- Access对子窗体数据进行批...(10.30)
- 最精简的组合框行来源数据快速输...(10.25)
- Access仿平台的多值选择器...(10.24)
- 【Access日期区间段查询】...(10.22)
- 【Access源码示例】VBA...(10.12)
- Access累乘示例,Acce...(10.09)
- 数值8.88,把整数8去掉,转...(10.08)
- 【Access自定义函数】一个...(09.30)
- 【Access选项卡示例】Ac...(09.09)
学习心得
最新文章
- Access快速开发平台企业版--...(11.18)
- 不会用多表联合查询,多表查询没结果...(11.16)
- 【案例分享】主键字段值含有不间断空...(11.16)
- Access快速开发平台--后台D...(11.14)
- 微软Access邀测新Monaco...(11.12)
- Access列表框左右互选、列表框...(11.11)
- 高效率在导入数据前删除记录(11.10)
- Access报价单转订单示例代码(11.08)
- Access系统自带的日期选择器不...(11.08)
- 分享一下Access工程中的acw...(11.07)