oracle如何用sql语句调取连续n天生产时间为0的数据
发布网友
发布时间:2022-04-13 07:33
我来回答
共2个回答
热心网友
时间:2022-04-13 09:02
--生成模拟数据
DROP TABLE t PURGE;
CREATE TABLE t AS
(SELECT TRUNC(SYSDATE+LEVEL) 日期,
round(dbms_random.value(0,2)) 生产时间
FROM DUAL
CONNECT BY LEVEL<=60);
--查询
SELECT MIN(日期) 开始日期, MAX(日期) 结束日期, COUNT(*) 连续天数
FROM (SELECT 生产时间,
日期,
ROW_NUMBER() OVER(ORDER BY 日期) 序号,
COUNT(1) OVER(PARTITION BY 生产时间 ORDER BY 日期) 按生产时间分组小计
FROM t)
WHERE 生产时间 = 0
GROUP BY 生产时间, 序号 - 按生产时间分组小计
HAVING COUNT(*) > 1
ORDER BY 1;
热心网友
时间:2022-04-13 10:20
可以百度“SQL解决过去M天内连续N天符合某条件问题”,chncaesar的帖子讨论的很详细了,这里只发一*现核心思想的语句。
with all_data as(
select ml.zoneno, ml.merch, date_id, nvl(tran_cnt,0) tran_cnt, row_number() over(partition by ml.zoneno, ml.merch order by date_id desc) rn from
(select acc.merch, dates.date_id, nvl(acc.cnt,0) tran_cnt from acc_tbl acc partition by (merch) right outer join (select trunc(sysdate) - level date_id from al connect by level <= (select max(last_days) from prm_tbl)) dates
on acc.d_date=dates.date_id) merch_dates partition by (merch, date_id) right outer join merch_list ml on merch_dates.merch=ml.merch
)
select * from all_data;