Oracle昨天没有返回任何数据

时间:2013-04-22 20:00:35

标签: sql oracle

我有以下查询:

SELECT d_dtm,
       BTS_ID,
       CASE WHEN D_DTM = (D_DTM-24/24)
            THEN sum(V_ATT_CNT)
       END AS "LASTATT",

       sum(V_ATT_CNT) as "V_ATT_CNT",

       CASE WHEN D_DTM = D_DTM
            THEN sum(V_ATT_CNT)
       END AS "ATT"

 FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI

WHERE to_date(D_DTM, 'DD/MM/yyyy') >=
      (SELECT TO_DATE(max(D_DTM),'DD/MM/YYYY') 
         FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI)-2

GROUP BY d_dtm,
        BTS_ID

HAVING CASE WHEN D_DTM = (D_DTM-24/24)
           THEN sum(V_ATT_CNT)                
       END > 0

但由于“having”条款,它没有返回任何结果。我知道它应该返回结果,因为我想要它做的是在一列中有当前时间段的V_ATT,而在第二列中,有24小时前的V_ATT。我检查了数据,我应该得到结果,但似乎无法弄清楚为什么这不起作用......

2 个答案:

答案 0 :(得分:0)

如果您小计(分组)D_DTM,则每个日期获得一行。如果要在同一行中将两个日期相互比较,则需要执行以下两项操作之一:子查询或窗口函数(如滞后/导致)。

我认为这就是你要找的东西:

SELECT d_dtm,
   BTS_ID,
   lag(sum(V_ATT_CNT), 1) over (partition by bts_id order by d_dtm) 
      AS "LASTATT",
   sum(V_ATT_CNT) as "V_ATT_CNT"

 FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI

  WHERE to_date(D_DTM, 'DD/MM/yyyy') >=
     (SELECT TO_DATE(max(D_DTM),'DD/MM/YYYY') 
         FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI)-2

 GROUP BY d_dtm,
    BTS_ID

请注意,滞后/结束中的partition by列是group by列的子集

答案 1 :(得分:0)

我使用with语句和两个查询重新编写了查询。工作得非常快,并给我正确的结果。

   WITH FRST
        AS (  SELECT D_DTM,
                     BSM_NM,
                     SUM (V_ATT_CNT) AS "V_ATT_CNT",
                     SUM (V_CUST_BLK_CNT) AS "V_CUST_BLK_CNT",
                     SUM (V_DRP_CALL_CNT) AS "V_DRP_CALL_CNT",
                     SUM (D_ATT_CNT) AS "D_ATT_CNT",
                     SUM (D_CUST_BLK_CNT) AS "D_CUST_BLK_CNT",
                     SUM (D_DRP_CALL_CNT) AS "D_DRP_CALL_CNT"
                FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI
               WHERE D_DTM >=
                        (SELECT MAX (D_DTM) - NUMTODSINTERVAL (12, 'HOUR')
                           FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI)
            GROUP BY D_DTM, BSM_NM),
        LST
        AS (  SELECT D_DTM,
                     BSM_NM,
                     SUM (V_ATT_CNT) AS "V_ATT_CNT",
                     SUM (V_CUST_BLK_CNT) AS "V_CUST_BLK_CNT",
                     SUM (V_DRP_CALL_CNT) AS "V_DRP_CALL_CNT",
                     SUM (D_ATT_CNT) AS "D_ATT_CNT",
                     SUM (D_CUST_BLK_CNT) AS "D_CUST_BLK_CNT",
                     SUM (D_DRP_CALL_CNT) AS "D_DRP_CALL_CNT"
                FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI
               WHERE D_DTM >=
                        (SELECT MAX (D_DTM) - NUMTODSINTERVAL (48, 'HOUR')
                           FROM DMSN.DS3R_FH_1XRTT_FA_LVL_KPI)
            GROUP BY D_DTM, BSM_NM),
        EVDOLST
        AS (  SELECT D_DTM,
                     BSM_NM,
                     SUM (ATT_CNT) AS "ATT_CNT",
                     SUM (CUST_BLK_CNT) AS "CUST_BLK_CNT",
                     SUM (DRP_CALL_CNT) AS "DRP_CALL_CNT"
                FROM DMSN.DS3R_FH_EVDO_FA_LVL_KPI
               WHERE D_DTM >=
                        (SELECT MAX (D_DTM) - NUMTODSINTERVAL (48, 'HOUR')
                           FROM DMSN.DS3R_FH_EVDO_FA_LVL_KPI)
            GROUP BY D_DTM, BSM_NM),
        EVDOFRST
        AS (  SELECT D_DTM,
                     BSM_NM,
                     SUM (ATT_CNT) AS "ATT_CNT",
                     SUM (CUST_BLK_CNT) AS "CUST_BLK_CNT",
                     SUM (DRP_CALL_CNT) AS "DRP_CALL_CNT"
                FROM DMSN.DS3R_FH_EVDO_FA_LVL_KPI
               WHERE D_DTM >=
                        (SELECT MAX (D_DTM) - NUMTODSINTERVAL (12, 'HOUR')
                           FROM DMSN.DS3R_FH_EVDO_FA_LVL_KPI)
            GROUP BY D_DTM, BSM_NM)
   SELECT t1.D_DTM,
          t1.BSM_NM,
          t2.V_ATT_CNT AS "LASTV_ATTCNT",
          t2.V_CUST_BLK_CNT AS "LASTV_BLKCNT",
          t2.V_DRP_CALL_CNT AS "LASTV_DRPCNT",
          t1.V_ATT_CNT AS "V_ATT_CNT",
          t1.V_CUST_BLK_CNT AS "V_CUST_BLK_CNT",
          t1.V_DRP_CALL_CNT AS "V_DRP_CALL_CNT",
          t2.D_ATT_CNT AS "LASTD_ATTCNT",
          t2.D_CUST_BLK_CNT AS "LASTD_BLKCNT",
          t2.D_DRP_CALL_CNT AS "LASTD_DRPCNT",
          t1.D_ATT_CNT AS "D_ATT_CNT",
          t1.D_CUST_BLK_CNT AS "D_CUST_BLK_CNT",
          t1.D_DRP_CALL_CNT AS "D_DRP_CALL_CNT",
          t3.ATT_CNT AS "EVDO_ATTCNT",
          t3.CUST_BLK_CNT AS "EVDO_BLKCNT",
          t3.DRP_CALL_CNT AS "EVDO_DRPCNT",
          t4.ATT_CNT AS "EVDO_LASTATTCNT",
          t4.CUST_BLK_CNT AS "EVDO_LASTBLKCNT",
          t4.DRP_CALL_CNT AS "EVDO_LASTDRPCNT"
     FROM FRST t1
          INNER JOIN LST t2
             ON     t1.BSM_NM = t2.BSM_NM
                AND t1.D_DTM - NUMTODSINTERVAL (24, 'HOUR') = t2.D_DTM
          LEFT OUTER JOIN EVDOLST t4
             ON     t1.BSM_NM = t4.BSM_NM
                AND t1.D_DTM - NUMTODSINTERVAL (24, 'HOUR') = t4.D_DTM
          LEFT OUTER JOIN EVDOFRST t3
             ON t1.BSM_NM = t3.BSM_NM AND t1.D_DTM = t3.D_DTM