sql - 基于日期运行计数器
问题描述
我有一张带有警报历史记录的表格,其中包含警报的开始日期、结束日期和原因。
我想为过去 30 天的每个日期计算当天发生的警报总数,这意味着如果警报从第 1 天开始并且仍在进行中(结束日期为空),那么它将计算从第 1 天到最后一天的所有天数.
这是我提出的查询
select cal.trunc_date,assets.group_id,
alert.*,
count( alert.asset_id)
over (PARTITION BY alert.REASON_ID ORDER BY
cal.trunc_date) TOTAL_ASSETS
from g_alert_history alert,
v_app_calendar cal,V_ACTIVE_ASSETS assets
where REASON_ID in (1,2)
and assets.asset_id=alert.asset_id
and assets.group_id=1462
and cal.trunc_date >= trunc(systimestamp - 30)
and alert.START_DATE_DEVICE >= trunc(systimestamp - 30)
and alert.START_DATE_DEVICE >= cal.trunc_date
and alert.START_DATE_DEVICE <= cal.trunc_date +1
and nvl (alert.END_DATE_DEVICE, systimestamp)
>=cal.trunc_date;
查看v_app_calendar
包含日期并V_ACTIVE_ASSETS
包含group_id
我要检查的日期。
问题是我得到重复,三次等。
结果如下:
TRUNC_DATE GROUP_ID REASON_ID ASSET_ID GEOFENCE_ID START_DATE_DEVICE END_DATE_DEVICE TOTAL_ASSETS
--------- -------- --------- -------- ----------- ------------------------------- ------------------------------- ------------
03-FEB-19 1462 1 1704 134 03-FEB-19 11.50.09.385000000 AM 13
03-FEB-19 1462 1 1704 134 03-FEB-19 11.55.09.475000000 AM 13
03-FEB-19 1462 1 1704 134 03-FEB-19 12.00.10.073000000 PM 13
03-FEB-19 1462 1 1704 134 03-FEB-19 12.05.11.126000000 PM 13
03-FEB-19 1462 1 1704 134 03-FEB-19 12.10.12.668000000 PM 13
03-FEB-19 1462 1 1704 134 03-FEB-19 12.15.12.858000000 PM 13
03-FEB-19 1462 1 1704 134 03-FEB-19 11.45.09.283000000 AM 13
03-FEB-19 1462 1 1704 134 03-FEB-19 11.20.03.587000000 AM 13
03-FEB-19 1462 1 1704 134 03-FEB-19 11.25.05.434000000 AM 13
03-FEB-19 1462 1 1704 134 03-FEB-19 11.30.07.294000000 AM 13
03-FEB-19 1462 1 1704 134 03-FEB-19 11.35.09.141000000 AM 13
03-FEB-19 1462 1 1704 134 03-FEB-19 11.40.09.251000000 AM 13
03-FEB-19 1462 1 1704 134 03-FEB-19 12.20.14.178000000 PM 13
05-FEB-19 1462 1 1663 134 05-FEB-19 02.33.02.475000000 PM 14
09-FEB-19 1462 1 1663 134 09-FEB-19 09.33.02.475000000 PM 09-FEB-19 11.33.22.475000000 PM 16
09-FEB-19 1462 1 1782 149 09-FEB-19 02.33.02.475000000 PM 09-FEB-19 02.36.02.475000000 PM 16
11-FEB-19 1462 1 2647 134 11-FEB-19 09.56.08.325000000 AM 140
11-FEB-19 1462 1 2647 164 11-FEB-19 09.56.08.325000000 AM 140
11-FEB-19 1462 1 2646 164 11-FEB-19 10.03.31.611000000 AM 140
11-FEB-19 1462 1 2646 134 11-FEB-19 10.03.31.611000000 AM 140
11-FEB-19 1462 1 1781 164 11-FEB-19 10.14.09.612000000 AM 140
11-FEB-19 1462 1 2647 134 11-FEB-19 11.55.20.281000000 AM 140
11-FEB-19 1462 1 1781 134 11-FEB-19 10.14.09.612000000 AM 140
11-FEB-19 1462 1 2647 164 11-FEB-19 10.55.32.300000000 AM 140
11-FEB-19 1462 1 1781 134 11-FEB-19 02.52.45.104000000 PM 140
11-FEB-19 1462 1 1781 164 11-FEB-19 03.20.40.461000000 PM 140
11-FEB-19 1462 1 1781 134 11-FEB-19 03.20.40.461000000 PM 140
11-FEB-19 1462 1 1781 164 11-FEB-19 08.28.13.331000000 PM 140
11-FEB-19 1462 1 1781 134 11-FEB-19 08.28.13.331000000 PM 140
11-FEB-19 1462 1 1781 134 11-FEB-19 03.20.42.461000000 PM 140
11-FEB-19 1462 1 1781 134 11-FEB-19 08.28.25.939000000 PM 140
11-FEB-19 1462 1 1781 164 11-FEB-19 08.28.25.939000000 PM 140
解决方案
如果您需要日级别的数据,则必须在将时间戳列转换为日期后应用 distinct 子句。
像下面这样的东西 -
select cal.trunc_date,assets.group_id,
alert.req_col,
cast(alert.start_date_device as date),
cast(alert.end_date_device as date)
count( alert.asset_id)
over (PARTITION BY alert.REASON_ID ORDER BY
cal.trunc_date) TOTAL_ASSETS
from g_alert_history alert,
v_app_calendar cal,V_ACTIVE_ASSETS assets
where REASON_ID in (1,2)
and assets.asset_id=alert.asset_id
and assets.group_id=1462
and cal.trunc_date >= trunc(systimestamp - 30)
and alert.START_DATE_DEVICE >= trunc(systimestamp - 30)
and alert.START_DATE_DEVICE >= cal.trunc_date
and alert.START_DATE_DEVICE <= cal.trunc_date +1
and nvl (alert.END_DATE_DEVICE, systimestamp)
>=cal.trunc_date;
您提供的数据不重复,因为它包含每条记录的唯一时间戳。
希望这可以帮助
推荐阅读
- centos - ceph mds服务启动centos 7失败
- c# - 如何使用模型 mvc 从数据表的第二页传递复选框值
- .net - 如何从树视图 WPF 中获取选定项目索引?
- javascript - 如何使用 js 选择器选择“::cue”元素并更改它?我正在尝试创建一个按钮来更改字幕不透明度
- c# - 单击复选框后如何打开新网页
- php - Prestashop 1.7.7.3 中未显示模块
- mongodb - 带有数组和排序的MongoDB复杂索引
- linux - 匿名内存映射 - 由程序的哪个部分使用?
- wordpress - 使用 ACF 的引导选项卡
- xamarin - 单击推送通知中的 Azure 推送通知问题