2013-04-22 55 views
1

返回任何數據我有以下查詢:甲骨文不是昨天小時

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 

但它沒有返回因爲「具有」條款的任何結果。我知道它應該返回結果,因爲我希望它所做的就是在一列中具有當前時間段的V_ATT,而在第二列中,在24小時前具有V_ATT。我檢查了數據,我應該得到結果,但似乎無法找出爲什麼這是行不通的...

+5

這種情況'D_DTM =(D_DTM-24/24)'總是FALSE。 – 2013-04-22 20:06:00

+1

您的查詢是錯誤的,很難閱讀。您需要添加示例數據。 – Art 2013-04-22 20:54:21

+0

什麼數據類型是'D_DTM'? – 2013-05-10 13:52:15

回答

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 
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

+0

我不認爲分區將在我的情況下工作,因爲通過查看行數進行分區... – user2061886 2013-05-10 13:44:55