如何找出缺少的單據編號?能否用一個Select語句實現?

magnetmoon 2003-11-11 04:51:08
單據編號(varchar(10))規則:前綴P+年+月+日+流水號(3位),
如何找出缺少的單據編號?能否用一個Select語句實現?
比如有以下數據:
P030101001
P030101002
P030101004
P030102001
P030102004
P030102005
則缺少的單據編號是:
P030101003
P030102002
P030102003
下面只是找到卻多少張,但是明細如何找到,請高手指點。
select *,convert(int,right(maxbh,3))-djsl as diff from
(
Select Left(tra_djbh1,7) as ymd,Count(distinct tra_djbh1) as djsl,Max(tra_djbh1) as maxbh
From tra_mstr
Where Left(tra_djbh1,7) Between 'T030101' and 'T030630'
Group by Left(tra_djbh1,7)
) as tmp
Where ymd+Replace(str(djsl,3),' ','0')<>maxbh
...全文
132 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
liweixin 2003-11-12
  • 打赏
  • 举报
回复
确实,你的想法很不错。
“能否先生成一個全編號的表,然后再join,如何生成一個全編號的表?”
生成这个表当然不成问题,但,如果根据那个已经产生的业务编号,去生成这个表,可能不大好(与实现无关,是想法的问题)。
如果时间已经是030204这天了,即使030201这天有缺号,你也不能考虑用呀。
所以,全编号表里边存999条数据,每天不同。这样,太加上你用JOIN的想法,就可以很好的解决你的问题了吧?!
说得比较简单,自已认为我清楚你想做什么。
LoveSQL 2003-11-12
  • 打赏
  • 举报
回复
TO:magnetmoon(天涯明月刀)
你的想法挺好的,我觉得可以这样来实现。
magnetmoon 2003-11-12
  • 打赏
  • 举报
回复
能否先生成一個全編號的表,然后再join,如何生成一個全編號的表?
如何根據
P030101004
P030102005
生成
P030101001
P030101002
P030101003
P030101004
P030102001
P030102002
P030102003
P030102004
P030102005
hillhx 2003-11-12
  • 打赏
  • 举报
回复
产生一个1到999的编码表
再产生一个年月日的编码表
两个编码表组合使用就可以了
zjcxc 元老 2003-11-11
  • 打赏
  • 举报
回复
结果:

日期标志 缺号记录
---------- ------------------------------------
P030101 P030101003
P030102 P030102002 -- P030102003

(所影响的行数为 2 行)
zjcxc 元老 2003-11-11
  • 打赏
  • 举报
回复
--下面是数据测试

--数据测试环境
declare @tb table(bh varchar(10))
insert into @tb
select 'P030101001'
union all select 'P030101002'
union all select 'P030101004'
union all select 'P030102001'
union all select 'P030102004'
union all select 'P030102005'

--生成临时表,得到数字编号及分组
select bhg=left(bh,7),bh=cast(right(bh,3) as int)
into #tb from @tb order by bhg,bh

--到每个缺号的开始编号
select id=identity(int,1,1),bhg,bid=bh+1
into #tb1 from #tb a
where not exists(select 1 from #tb where bh=a.bh+1 and bhg=a.bhg)

--得到每个缺号的结束编号
select id=identity(int,0,1),bhg,eid=bh-1
into #tb2 from #tb a
where not exists(select 1 from #tb where bh=a.bh-1 and bhg=a.bhg)

--显示缺号结果
select 日期标志=a.bhg
,缺号记录=a.bhg+right('000'+cast(a.bid as varchar),3)
+case a.bid when b.eid then ''
else ' -- '+b.bhg+right('000'+cast(b.eid as varchar),3) end
from #tb1 a inner join #tb2 b
on a.id=b.id and a.bhg=b.bhg

go
--删除临时表
drop table #tb,#tb1,#tb2
zjcxc 元老 2003-11-11
  • 打赏
  • 举报
回复
--生成临时表,得到数字编号及分组
select bhg=left(bh,7),bh=cast(right(bh,3) as int)
into #tb from 表 order by bhg,bh

--到每个缺号的开始编号
select id=identity(int,1,1),bhg,bid=bh+1
into #tb1 from #tb a
where not exists(select 1 from #tb where bh=a.bh+1 and bhg=a.bhg)

--得到每个缺号的结束编号
select id=identity(int,0,1),bhg,eid=bh-1
into #tb2 from #tb a
where not exists(select 1 from #tb where bh=a.bh-1 and bhg=a.bhg)

--显示缺号结果
select 日期标志=a.bhg
,缺号记录=a.bhg+right('000'+cast(a.bid as varchar),3)
+case a.bid when b.eid then ''
else ' -- '+b.bhg+right('000'+cast(b.eid as varchar),3) end
from #tb1 a inner join #tb2 b
on a.id=b.id and a.bhg=b.bhg

go
--删除临时表
drop table #tb,#tb1,#tb2
gmlxf 2003-11-11
  • 打赏
  • 举报
回复
varchar类型的较难办,要通过转换后才可以。
如果是数字那是很简单了。。

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧