db2生成连续日期
//生成时间段内连续日期
select * from
( select date('2008-01-01') + (row_NUMBER() over () -1) days AS datennn
from sysibm.syscoldist )
where datennn < date ('2013-01-01')
//生成时间段内所有节点的笛卡尔积
select a.COMP_NAME, a.COMP_ID,b.datennn from MANAGE8_BASE_NODE_INFO a,
(
select * from
( select date('2017-01-01') + (row_NUMBER() over () -1) days AS datennn
from sysibm.syscoldist )
where datennn < date ('2017-01-30')
) b
where a.NODE_TYPE='0005
//查询出进场数据为空的节点数据
select c.NODE_NAME,c.NODE_ID,c.COLLECT_DATE,COALESCE(b.VEG_IN_COUNT,0),COALESCE(b.MEAT_IN_COUNT,0) from (
select a.COMP_NAME as NODE_NAME , a.COMP_ID as NODE_ID,b.datennn as COLLECT_DATE from MANAGE8_BASE_NODE_INFO a,
(
select * from
( select date('2017-01-01') + (row_NUMBER() over () -1) days AS datennn
from sysibm.syscoldist )
where datennn < date ('2017-01-30')
) b
where a.NODE_TYPE='0005'
) c left join
(select * from NODE_INFO_COLLECT where COLLECT_DATE >= '2017-01-01 00:00:00' and COLLECT_DATE <'2017-02-01 00:00:00' and NODE_TYPE='0005' ) b
on c.NODE_ID= b.NODE_ID
//下面是最终的sql,还没测试,有待修改
select DISTINCT tstart.NODE_ID,tstart.NODE_NAME,tstart.COLLECT_DATE from NODE_INFO_COLLECT as tstart ,NODE_INFO_COLLECT as tend
where tstart.NODE_ID = tend.NODE_ID
and (tstart.VEG_IN_COUNT+tstart.MEAT_IN_COUNT ) =0
and (tend.VEG_IN_COUNT+tend.MEAT_IN_COUNT ) >0
and tstart.NODE_TYPE='0005' and tend.NODE_TYPE='0005'
and tend.COLLECT_DATE >= DATE(tstart.COLLECT_DATE) + 3 days
and EXISTS (SELECT 1 FROM NODE_INFO_COLLECT AS TGood
WHERE TGood.NODE_ID = tstart.NODE_ID
AND (TGood.VEG_IN_COUNT+TGood.MEAT_IN_COUNT ) =0
AND TGood.COLLECT_DATE BETWEEN tstart.COLLECT_DATE AND tend.COLLECT_DATE
HAVING COUNT(TGood.NODE_ID) >=3)
AND NOT EXISTS (SELECT 1 FROM NODE_INFO_COLLECT AS TBad
WHERE TBad.NODE_ID = TStart.NODE_ID
AND (TBad.VEG_IN_COUNT+TBad.MEAT_IN_COUNT ) >0
AND TBad.COLLECT_DATE BETWEEN tstart.COLLECT_DATE AND tend.COLLECT_DATE)
//最后汇总sql,有待改进,执行效率慢
select DISTINCT tstart.NODE_ID,tstart.NODE_NAME,tstart.COLLECT_DATE
from
(
select tab1.NODE_NAME,tab1.NODE_ID,COALESCE(tab2.VEG_IN_COUNT,0) VEG_IN_COUNT,COALESCE(tab2.MEAT_IN_COUNT,0) MEAT_IN_COUNT,tab1.COLLECT_DATE from (
select c.NODE_NAME,c.NODE_ID,c.COLLECT_DATE from (
select a.COMP_NAME as NODE_NAME , a.COMP_ID as NODE_ID,b.datennn as COLLECT_DATE from MANAGE8_BASE_NODE_INFO a,
(
select * from
( select date('2017-01-01') + (row_NUMBER() over () -1) days AS datennn
from sysibm.syscoldist )
where datennn < date ('2017-02-01')
) b
where a.COMP_ID='410105095'
) c
) tab1 left join
(
select NODE_NAME,NODE_ID,VEG_IN_COUNT,MEAT_IN_COUNT,COLLECT_DATE from NODE_INFO_COLLECT
where COLLECT_DATE >= '2017-01-01 00:00:00' and COLLECT_DATE <'2017-02-01 00:00:00' and NODE_ID='410105095'
) tab2
on tab1.NODE_ID=tab2.NODE_ID and date(tab1.COLLECT_DATE) = date(tab2.COLLECT_DATE)
) tstart ,
(
select tab1.NODE_NAME,tab1.NODE_ID,COALESCE(tab2.VEG_IN_COUNT,0) VEG_IN_COUNT,COALESCE(tab2.MEAT_IN_COUNT,0) MEAT_IN_COUNT,tab1.COLLECT_DATE from (
select c.NODE_NAME,c.NODE_ID,c.COLLECT_DATE from (
select a.COMP_NAME as NODE_NAME , a.COMP_ID as NODE_ID,b.datennn as COLLECT_DATE from MANAGE8_BASE_NODE_INFO a,
(
select * from
( select date('2017-01-01') + (row_NUMBER() over () -1) days AS datennn
from sysibm.syscoldist )
where datennn < date ('2017-02-01')
) b
where a.COMP_ID='410105095'
) c
) tab1 left join
(
select NODE_NAME,NODE_ID,VEG_IN_COUNT,MEAT_IN_COUNT,COLLECT_DATE from NODE_INFO_COLLECT
where COLLECT_DATE >= '2017-01-01 00:00:00' and COLLECT_DATE <'2017-02-01 00:00:00' and NODE_ID='410105095'
) tab2
on tab1.NODE_ID=tab2.NODE_ID and date(tab1.COLLECT_DATE) = date(tab2.COLLECT_DATE)
) tend
where tstart.NODE_ID = tend.NODE_ID
and (tstart.VEG_IN_COUNT+tstart.MEAT_IN_COUNT ) =0
and (tend.VEG_IN_COUNT+tend.MEAT_IN_COUNT ) =0
and DATE(tend.COLLECT_DATE) = DATE(tstart.COLLECT_DATE) + 1 days
and EXISTS (SELECT 1 FROM
(
select tab1.NODE_NAME,tab1.NODE_ID,COALESCE(tab2.VEG_IN_COUNT,0) VEG_IN_COUNT,COALESCE(tab2.MEAT_IN_COUNT,0) MEAT_IN_COUNT,tab1.COLLECT_DATE from (
select c.NODE_NAME,c.NODE_ID,c.COLLECT_DATE from (
select a.COMP_NAME as NODE_NAME , a.COMP_ID as NODE_ID,b.datennn as COLLECT_DATE from MANAGE8_BASE_NODE_INFO a,
(
select * from
( select date('2017-01-01') + (row_NUMBER() over () -1) days AS datennn
from sysibm.syscoldist )
where datennn < date ('2017-02-01')
) b
where a.COMP_ID='410105095'
) c
) tab1 left join
(
select NODE_NAME,NODE_ID,VEG_IN_COUNT,MEAT_IN_COUNT,COLLECT_DATE from NODE_INFO_COLLECT
where COLLECT_DATE >= '2017-01-01 00:00:00' and COLLECT_DATE <'2017-02-01 00:00:00' and NODE_ID='410105095'
) tab2
on tab1.NODE_ID=tab2.NODE_ID and date(tab1.COLLECT_DATE) = date(tab2.COLLECT_DATE)
)
TGood
WHERE TGood.NODE_ID = tstart.NODE_ID
AND (TGood.VEG_IN_COUNT+TGood.MEAT_IN_COUNT ) =0
AND TGood.COLLECT_DATE BETWEEN tstart.COLLECT_DATE AND tend.COLLECT_DATE
HAVING COUNT(TGood.NODE_ID) >=2)
AND NOT EXISTS (SELECT 1 FROM
(
select tab1.NODE_NAME,tab1.NODE_ID,COALESCE(tab2.VEG_IN_COUNT,0) VEG_IN_COUNT,COALESCE(tab2.MEAT_IN_COUNT,0) MEAT_IN_COUNT,tab1.COLLECT_DATE from (
select c.NODE_NAME,c.NODE_ID,c.COLLECT_DATE from (
select a.COMP_NAME as NODE_NAME , a.COMP_ID as NODE_ID,b.datennn as COLLECT_DATE from MANAGE8_BASE_NODE_INFO a,
(
select * from
( select date('2017-01-01') + (row_NUMBER() over () -1) days AS datennn
from sysibm.syscoldist )
where datennn < date ('2017-02-01')
) b
where a.COMP_ID='410105095'
) c
) tab1 left join
(
select NODE_NAME,NODE_ID,VEG_IN_COUNT,MEAT_IN_COUNT,COLLECT_DATE from NODE_INFO_COLLECT
where COLLECT_DATE >= '2017-01-01 00:00:00' and COLLECT_DATE <'2017-02-01 00:00:00' and NODE_ID='410105095'
) tab2
on tab1.NODE_ID=tab2.NODE_ID and date(tab1.COLLECT_DATE) = date(tab2.COLLECT_DATE)
)
TBad
WHERE TBad.NODE_ID = TStart.NODE_ID
AND (TBad.VEG_IN_COUNT+TBad.MEAT_IN_COUNT ) >0
AND TBad.COLLECT_DATE BETWEEN tstart.COLLECT_DATE AND tend.COLLECT_DATE)
//加入临时物理表
create table TEST_NODE_COUNT as (
select tab1.NODE_NAME,tab1.NODE_ID,COALESCE(tab2.VEG_IN_COUNT,0) VEG_IN_COUNT,COALESCE(tab2.MEAT_IN_COUNT,0) MEAT_IN_COUNT,tab1.COLLECT_DATE from (
select c.NODE_NAME,c.NODE_ID,c.COLLECT_DATE from (
select a.COMP_NAME as NODE_NAME , a.COMP_ID as NODE_ID,b.datennn as COLLECT_DATE from MANAGE8_BASE_NODE_INFO a,
(
select * from
( select date('2017-01-01') + (row_NUMBER() over () -1) days AS datennn
from sysibm.syscoldist )
where datennn < date ('2017-02-01')
) b
where a.NODE_TYPE='0005'
) c
) tab1 left join
(
select NODE_NAME,NODE_ID,VEG_IN_COUNT,MEAT_IN_COUNT,COLLECT_DATE from NODE_INFO_COLLECT
where COLLECT_DATE >= '2017-01-01 00:00:00' and COLLECT_DATE <'2017-02-01 00:00:00' and NODE_TYPE='0005'
) tab2
on tab1.NODE_ID=tab2.NODE_ID and date(tab1.COLLECT_DATE) = date(tab2.COLLECT_DATE)
) definition only
select * from test_node_count
delete * from test_node_count
//核对信息
select * from (
select DISTINCT tstart.NODE_ID,tstart.NODE_NAME,tstart.COLLECT_DATE
from test_node_count tstart ,
(
select * from test_node_count
) tend
where tstart.NODE_ID = tend.NODE_ID
and (tstart.VEG_IN_COUNT+tstart.MEAT_IN_COUNT ) =0
and (tend.VEG_IN_COUNT+tend.MEAT_IN_COUNT ) =0
and DATE(tend.COLLECT_DATE) = DATE(tstart.COLLECT_DATE) + 2 days
and EXISTS (SELECT 1 FROM test_node_count TGood
WHERE TGood.NODE_ID = tstart.NODE_ID
AND (TGood.VEG_IN_COUNT+TGood.MEAT_IN_COUNT ) =0
AND TGood.COLLECT_DATE BETWEEN tstart.COLLECT_DATE AND tend.COLLECT_DATE
HAVING COUNT(TGood.NODE_ID) >=3)
AND NOT EXISTS (SELECT 1 FROM test_node_count TBad
WHERE TBad.NODE_ID = TStart.NODE_ID
AND (TBad.VEG_IN_COUNT+TBad.MEAT_IN_COUNT ) >0
AND TBad.COLLECT_DATE BETWEEN tstart.COLLECT_DATE AND tend.COLLECT_DATE)
) as tab where tab.node_id='410103023'
db2生成连续日期的更多相关文章
- sql生成连续日期(年份、月份、日期)
此随笔主在分享日常可能用到的sql函数,用于生成连续日期(年份.月份.日期) 具体的看代码及效果吧! -- ============================================= ...
- sql server使用公用表表达式CTE通过递归方式编写通用函数自动生成连续数字和日期
问题:在数据库脚本开发中,有时需要生成一堆连续数字或者日期,例如yearly report就需要连续数字做年份,例如daily report就需要生成一定时间范围内的每一天日期.而自带的系统表mast ...
- SQL经典问题 找出连续日期及连续的天数
转自:http://bbs.csdn.net/topics/360019248 如何取到每段连续日期的起始终止日期以及持续天数及起始日期距上一期终止日期的天数,能否用一句sql实现?备注:数据库环境是 ...
- SQL 生成一个日期范围
有时想按日或月生成一个序列,就像2014-1-1.2014-1-2.2014-1-3... 在sql server中可以写个函数来实现. /* 生成一个日期范围,如2014.01.2014.02... ...
- oracle行转列、列转行、连续日期数字实现方式及mybatis下实现方式
转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9977591.html 九月份复习,十月份考试,十月底一直没法收心,赶在十一初 由于不可抗拒的原因又不得不重新找 ...
- python生成随机日期字符串
python生成随机日期字符串 生成随机的日期字符串,用于插入数据库. 通过时间元组设定一个时间段,开始和结尾时间转换成时间戳. 时间戳中随机取一个,再生成时间元组,再把时间元组格式化输出为字符串 # ...
- MSSQL高并发下生成连续不重复的订单号
一.确定需求 只要做过开发的基本上都有做过订单,只要做过订单的基本上都要涉及生成订单号,可能项目订单号生成规则都不一样,但是大多数规则都是连续增长. 所以假如给你一个这样的需求,在高并发下,以天为单位 ...
- oracle 生成随机日期+时间
oracle 生成随机日期+时间 SELECT to_date(TRUNC(DBMS_RANDOM.VALUE(to_number(to_char(to_date('20110101','yyyymm ...
- 在论坛中出现的比较难的sql问题:33(递归 连续日期问题 )
原文:在论坛中出现的比较难的sql问题:33(递归 连续日期问题 ) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有必 ...
随机推荐
- 005-MYSQL数据库设计原则
1.核心原则 不在数据库做运算; cpu计算务必移至业务层; 控制列数量(字段少而精,字段数建议在20以内); 平衡范式与冗余(效率优先:往往牺牲范式) 拒绝3B(拒绝大sql语句:big sql.拒 ...
- range开始节点和结束节点
sc = range.startContainer, so = range.startOffsest ec = range.endContainer, eo=range.endOffset sta ...
- 建议47:使用logging记录日志信息
# -*- coding:utf-8 -*- ''' Python中自带的logging 模块提供了日志功能,它将logger 的level 分为5 个级别 DEBUG 详细的信息,在追踪问题的时候使 ...
- CSS3动画表单
在线演示 本地下载
- volatile笔记
总结自:https://www.cnblogs.com/dolphin0520/p/3920373.html 了解volatile之前得明白什么是原子性.可见性.有序性及指令重排序,详见:https: ...
- iOS_触摸事件与手势识别
目 录: 一.触摸事件 1.1iOS的输入事件 1.2 触摸事件的处理 1.3 UITouch类中包含五个属性 1.4 UITouch类中包含两个成员函数 1.5响应者链 二.手势识别 2.1使用手 ...
- C# 利用TTS实现文本转语音
TSS(Text To Speech),语音朗读文本的技术,在Windows下台下,微软给我们提供了一套API接口(Speech API).金山词霸的朗读功能就是用的这个接口. WindowsXP自带 ...
- 微服务(MicroServices)
微服务Architecture(MicroServices) 微服务架构简单的定义 采用一组Service的方式来构建一个应用,服务独立部署在不同的进程(Container)中,不同Service通过 ...
- Windows Server 2008 IIS的备份和还原
Windows Server 2008 IIS的备份和还原 当我们服务器系统有大量的站点和虚拟目录的时候,因为种种原因需要重做系统,那么重装系统后这些站点我们是否只能一个一个的添加,如果有成百上千个站 ...
- 【P3522】TEM(单调队列+DP)
这个题,题目很长,然而亲爱的翻译已经帮你读完题了,一句话题意. 要求不下降的最长,那么这一段肯定满足队首的左区间不大于队尾的右区间,单调队列容易求解. #include<iostream> ...