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问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有必 ...
随机推荐
- 在GCE上安装Apache、tomcat等
1.安装Apache2.2.3 (虚机的操作系统是CentOS7) sudo yum install wget -y cd /opt sudo wget http://archive.apache.o ...
- Android sdk manager加载缓慢或加载不出来
1.打开android sdk manager 2.打开tool->options,如图所示 3.将Proxy Settings 里的HTTP Proxy Server和HTTP Proxy P ...
- asp.net 文件上传
前台js <script type="text/javascript"> window.onload = function () { document.getEleme ...
- 【HackerRank】Ice Cream Parlor
Sunny and Johnny together have M dollars which they intend to use at the ice cream parlour. Among N ...
- 【HackerRank】 Find Digits
Find Digits Problem Statement Given a number you have to print how many digits in that number exactl ...
- Java变量修饰符volatile
volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...
- Bürkert 流体控制系统 (8611 型通用调节器)
Type Description High-Tech Made EasyThe new universal controller eCONTROL Type 8611 brings an essent ...
- 主攻ASP.NET.4.5.1 MVC5.0之重生:空地搭建一个包含 Ninject框架 项目
1.创建一个空白解决方案 2.添加一个类库 名称为XXX.Domain 3.添加一个ASP.MVC 名称为XXX.WebUI 4.选着空模版,勾选MVC核心引用 5.添加单元测试项目XXX.UntiT ...
- 三、golang时间、流程控、函数
一.本篇内容 1.string和strconv使用 2.go中的时间和日期类型 3.流程控制 4.函数讲解 二.string和strconv使用 1. string.HasPrefix(s trin ...
- 五、golang实现排序
实现排序: 1.实现一个冒泡排序 2.实现一个选择排序 3.实现一个插入排序 4.实现一个快速排序 冒泡排序 package main import( "fmt" ) func b ...