【问题标题】:Running Counter Based On Date基于日期运行计数器
【发布时间】:2019-02-16 06:25:04
【问题描述】:

我有一个包含警报历史记录的表格,其中包含开始日期、结束日期和警报原因。

我想为过去 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

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    如果您需要日级别的数据,则必须在将时间戳列转换为日期后应用 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;
    

    您提供的数据不重复,因为它包含每个记录的唯一时间戳。

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      试试下面的代码。

      日期表包含过去 30 天的所有日期,包括今天。

      我还将您的 JOIN 语法更改为更新的形式。

      with dates as (
          select trunc(sysdate) - (level - 1) trunc_date from dual connect by level<=30
      )
      select dates.trunc_date
           , count(alert.asset_id)
        from g_alert_history alert
        join v_app_calendar cal
          on (alert.START_DATE_DEVICE between cal.trunc_date and (cal.trunc_date +1)
              and nvl (alert.END_DATE_DEVICE, systimestamp) >= cal.trunc_date )
        join V_ACTIVE_ASSETS assets
          on (assets.asset_id=alert.asset_id)
       where REASON_ID in (1,2)
         and dates.trunc_date between trunc(alert.START_DATE_DEVICE) and nvl(alert.END_DATE_DEVICE, trunc(sysdate))
         and cal.trunc_date >= trunc(systimestamp - 30)
         and assets.group_id=1462
       group by dates.trunc_date
      

      希望我能帮上忙!

      【讨论】:

        【解决方案3】:

        由于您希望对当天发生的所有警报进行每日计数(警报可能在前一天开始或在未来一天结束),因此您希望使用按天分组的聚合计数(可能还有其他标准),而不是您在查询中显示的分析计数。要在给定日期不产生过多重复项的情况下执行聚合,您需要消除提供非不同值的列。主要是提醒的开始和结束日期,以及asset_idgeofence_id

        下面的查询将为您提供过去 30 天每天发生的请求的 group_idreason_ids 发生的更改计数。

        select cal.trunc_date
             , assets.group_id
             , alert.reason_id
             , count( alert.asset_id) TOTAL_ASSETS
          from g_alert_history alert
          join V_ACTIVE_ASSETS assets
            on assets.asset_id=alert.asset_id
          join v_app_calendar cal
            on alert.START_DATE_DEVICE < cal.trunc_date + 1
           and (alert.END_DATE_DEVICE is null or cal.trunc_date <= alert.END_DATE_DEVICE)
         where alert.REASON_ID in (1,2)
           and assets.group_id=1462
           and cal.trunc_date between trunc(sysdate - 30) and sysdate
         group by cal.trunc_date
             , assets.group_id
             , alert.reason_id
         order by cal.trunc_date
             , assets.group_id
             , alert.reason_id;
        

        【讨论】:

          猜你喜欢
          • 2021-12-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-07
          • 2019-02-14
          • 2017-03-15
          相关资源
          最近更新 更多