本帖最后由 jizhanyong72 于 2011-11-30 23:36 编辑
oracle数据库表字段:条形码,库存,时间;查询条件:起始时间、截至时间、库存为0;
比如在时间段内有30条记录,需求是,从第10条开始,下面的20条记录里每条记录库存都为0,结果为20,前10条记录可能也有库存为0,只是不是连续的,就是统计从那条记录开始,连续的记录都符合条件的计数;
各位大侠,SQL语句该怎么写,或者PL/SQL 该怎么写?
表:TEST
数据模版:
TXM KUCUN TIME
1 001 0 20111101
2 001 2 20111102
3 001 0 20111103
4 001 0 20111104
5 001 5 20111105
6 001 0 20111106
7 001 0 20111107
8 001 0 20111108
9 001 0 20111109
10 001 0 20111110
11 004 0 20111110
12 002 0 20111101
13 002 2 20111102
14 002 0 20111103
15 002 3 20111104
16 002 5 20111105
17 002 0 20111106
18 002 0 20111107
19 003 0 20111108
20 003 0 20111109
21 003 0 20111110
我想得到的结果是 TXM COUNTNUM
001 5
002 2
003 3
就是TXM:001从第6行开始到10行的统计,KUCUN列值一直到001结尾连续为0且最大时间(最晚时间)的统计,不管之前有多少连续为0;从最后一次不是0记起,一直到最后一次时间,之间的统计个数 002从第16行开始到17行的统计,KUCUN列值一直到002结尾连续为0且最大时间(最晚时间)的统计,不管之前有多少连续为0;从最后一次不是0记起,一直到最后一次时间,之间的统计个数
003从第18行开始到20行的统计,KUCUN列值一直到003结尾连续为0且最大时间(最晚时间)的统计,不管之前有多少连续为0;从最后一次不是0记起,一直到最后一次时间,之间的统计个数
创建表结构SQL:
CREATE TABLE "TEST"
( "TXM" VARCHAR2(50),
"KUCUN" VARCHAR2(50),
"TIME" VARCHAR2(50)
)
添加数据SQL:
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('001', '0', '20111101', 'AAAHzCAALAAAAkWAAA');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('001', '2', '20111102', 'AAAHzCAALAAAAkWAAB');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('001', '0', '20111103', 'AAAHzCAALAAAAkWAAC');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('001', '0', '20111104', 'AAAHzCAALAAAAkWAAD');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('001', '5', '20111105', 'AAAHzCAALAAAAkWAAE');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('001', '0', '20111106', 'AAAHzCAALAAAAkWAAF');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('001', '0', '20111107', 'AAAHzCAALAAAAkWAAG');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('001', '0', '20111108', 'AAAHzCAALAAAAkWAAH');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('001', '0', '20111109', 'AAAHzCAALAAAAkWAAI');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('001', '0', '20111110', 'AAAHzCAALAAAAkWAAJ');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('004', '0', '20111110', 'AAAHzCAALAAAAkWAAK');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('002', '0', '20111101', 'AAAHzCAALAAAAkXAAA');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('002', '2', '20111102', 'AAAHzCAALAAAAkXAAB');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('002', '0', '20111103', 'AAAHzCAALAAAAkXAAC');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('002', '3', '20111104', 'AAAHzCAALAAAAkXAAD');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('002', '5', '20111105', 'AAAHzCAALAAAAkXAAE');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('002', '0', '20111106', 'AAAHzCAALAAAAkXAAF');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('002', '0', '20111107', 'AAAHzCAALAAAAkXAAG');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('003', '0', '20111108', 'AAAHzCAALAAAAkXAAH');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('003', '0', '20111109', 'AAAHzCAALAAAAkXAAI');
insert into TEST (TXM, KUCUN, TIME, ROWID)
values ('003', '0', '20111110', 'AAAHzCAALAAAAkXAAJ');