17,086
社区成员
发帖
与我相关
我的任务
分享
with tab1 as (
select 'A' id, to_date('2018-04-01', 'yyyy-mm-dd') in_time, 3 cnt from dual union all
select 'A' id, to_date('2018-09-01', 'yyyy-mm-dd') in_time, 3 cnt from dual union all
select 'A' id, to_date('2018-10-03', 'yyyy-mm-dd'), 5 from dual union all
select 'A' id, to_date('2018-11-04', 'yyyy-mm-dd'), 5 from dual union all
select 'A' id, to_date('2018-11-02', 'yyyy-mm-dd'), 1 from dual
)
,tab2 as (
select t1.*,
sum(t1.cnt) over(order by t1.in_time desc) sum_cnt
from tab1 t1
)
,tab3 as (
select t1.*,
case when 7 >= sum_cnt
then cnt
when 7 >= lead(sum_cnt) over(order by t1.in_time)
then 7 - lead(sum_cnt) over(order by t1.in_time)
else 0
end result_
from tab2 t1
)
,tab4 as (
select sysdate - level * 30 st, sysdate - (level - 1) * 30 ed, level lv from dual connect by level <= 4
)
select sum(decode(t2.lv, 1, result_, 0)) "30天内",
sum(decode(t2.lv, 2, result_, 0)) "60天内",
sum(decode(t2.lv, 3, result_, 0)) "90天内",
sum(decode(t2.lv, 4, result_, 0)) "90以上"
from tab3 t1, tab4 t2
where (t1.in_time between t2.st and t2.ed and t2.lv != 4)
or (t1.in_time < t2.ed and t2.lv = 4)
;
--入库单主表
Create Global Temporary TABLE t1 (rid int,code char(10),intime date)
INSERT INTO t1 VALUES (1,'A', to_date(1,'rk001','2018-09-01 18:31:34','YYYY-MM-DD HH24:MI:SS' ))
INSERT INTO t1 VALUES (2,'A', to_date(2,'rk002','2018-10-03 09:31:34','YYYY-MM-DD HH24:MI:SS' ))
INSERT INTO t1 VALUES (3,'A', to_date(1,'rk003','2018-11-02 18:31:34','YYYY-MM-DD HH24:MI:SS' ))
--入库单子表,子表入库单号对应到主表
Create Global Temporary TABLE t2 (rid INT, in_code char(10), productName CHAR(10),inCnt INT)
INSERT INTO t2 VALUES (1,'rk001','A',3)
INSERT INTO t2 VALUES (2,'rk002','A',5)
INSERT INTO t2 VALUES (3,'rk003','A',6)
--计算后的当前结存表,假如当前库存数为7
Create Global Temporary t3 TABLE (productName NVARCHAR(10),jcCnt INT)
INSERT INTO @t2 VALUES ('A',7)
with tab1 as (
select 'A' id, to_date('2018-04-01', 'yyyy-mm-dd') in_time, 3 cnt from dual union all
select 'A' id, to_date('2018-09-01', 'yyyy-mm-dd') in_time, 3 cnt from dual union all
select 'B' id, to_date('2018-09-01', 'yyyy-mm-dd') in_time, 3 cnt from dual union all
select 'A' id, to_date('2018-10-03', 'yyyy-mm-dd'), 5 from dual union all
select 'A' id, to_date('2018-11-04', 'yyyy-mm-dd'), 5 from dual union all
select 'B' id, to_date('2018-11-04', 'yyyy-mm-dd'), 5 from dual union all
select 'A' id, to_date('2018-11-02', 'yyyy-mm-dd'), 1 from dual
)
,tab2 as (
select t1.*,
sum(t1.cnt) over(partition by t1.id order by t1.in_time desc) sum_cnt
from tab1 t1
)
,tab3 as (
select t1.*,
case when 7 >= sum_cnt
then cnt
when 7 >= lead(sum_cnt) over(partition by t1.id order by t1.in_time)
then 7 - lead(sum_cnt) over(partition by t1.id order by t1.in_time)
else 0
end result_
from tab2 t1
)
,tab4 as (
select sysdate - level * 30 st, sysdate - (level - 1) * 30 ed, level lv from dual connect by level <= 4
)
select t1.id,
sum(decode(t2.lv, 1, result_, 0)) "30天内",
sum(decode(t2.lv, 2, result_, 0)) "60天内",
sum(decode(t2.lv, 3, result_, 0)) "90天内",
sum(decode(t2.lv, 4, result_, 0)) "90以上"
from tab3 t1, tab4 t2
where (t1.in_time between t2.st and t2.ed and t2.lv != 4)
or (t1.in_time < t2.ed and t2.lv = 4)
group by t1.id
;