求sql语句

zymhdboy 2009-04-02 12:21:06
表如下
voucherno status
00001 0
00002 1
00003 0
00004 0
00005 1
00006 1
00007 1
类似于银行存折的状态,0代表库存,1代表已用,其他的还有作废、注销等
想做一个数据统计的SQL,获取的结果如下
起始印刷号 终止印刷号 数量 状态
00001 00001 1 0
00002 00002 1 1
00003 00004 2 0
00005 00007 3 1
...全文
128 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangsong145 2009-04-02
  • 打赏
  • 举报
回复
你的起始和终止印刷号是怎么区分的啊?
Jane_64 2009-04-02
  • 打赏
  • 举报
回复
select 起始印刷号,e.voucherno 终止印刷号,终止印刷号-起始印刷号+1 数量, e.status 状态 from (
select min(起始印刷号) 起始印刷号,终止印刷号 from (
select a.voucherno 起始印刷号,
isnull((select min(voucherno) - 1 from
t_voucher b
where b.voucherno >a.voucherno and a.status <> b.status),
(select max(voucherno) from t_voucher)) 终止印刷号
from t_voucher a) c
group by 终止印刷号) d,t_voucher e
where d.终止印刷号 = e.voucherno
jdsnhan 2009-04-02
  • 打赏
  • 举报
回复
select min(voucherno),max(voucherno),count(*),status
from
(select status,voucherno,to_number(voucherno)-row_number() over(partition by status order by voucherno) rn from a)
group by status,rn
order by 1
zymhdboy 2009-04-02
  • 打赏
  • 举报
回复
就是按存折状态来区分,比如00001、00003号的状态都为0,但00002状态为1,中间断开了,则00001既是起始印刷号也是终止印刷号,00003既是起始也是终止印刷号,而00005、00006、00007的状态为0,并且这三个印刷号连续,则起始印刷号为00005,终止印刷号为00007

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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