几种精妙的断号查询方法
时 间: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.08)
- 分享一下Access工程中的acw...(11.07)
- Access快速开发平台--让有权...(11.04)
- Access快速开发平台--审批选...(11.01)
- ACCESS两张表先各自排序,然后...(10.31)
- Access对子窗体数据进行批量+...(10.30)
- SqlServer中如何用SQL命...(10.29)
- Access报表中的分组功能用代码...(10.28)
- 用Access计算库存结余的一个方...(10.26)
- 最精简的组合框行来源数据快速输入(...(10.25)