2013-03-26 91 views
0

我無法弄清楚如何修改我的查詢以使用information.infoDate和otherDates.auxDate之間的最新日期。SQL Case語句子選擇按子查詢分組

SELECT state, COUNT(type) as EQUIPMENT, 
     (SELECT COUNT(type) 
     FROM information a 
     WHERE a.state = b.state 
       AND storedprocedures.to_date(infoDate) < (SELECT current date - 5 days 
                 FROM sysibm.sysdummy1) 
     GROUP BY state) as IDLE, 
     ROUND((SELECT COUNT(type) 
       FROM information a 
       WHERE a.state = b.state 
        AND storedprocedures.to_date(infoDate) < (SELECT current date - 5 days 
                   FROM sysibm.sysdummy1) 
       GROUP BY state)/CAST(COUNT(type) as float) * 100, 1) as percent 
FROM information b 
JOIN validStates 
    ON state = vstate 
LEFT JOIN otherDates 
    ON typeid = auxtypeid 
WHERE state <> '***' 
GROUP BY state 
ORDER BY state 

我試過使用下面的語句,而不是infoDate的各種方式,但我無法得到查詢運行。有沒有更好的方法來使用這兩個日期中最新的?

SELECT CASE WHEN auxDate > infoDate 
      THEN auxDate 
      ELSE infoDate END AS activityDate 
FROM information 
LEFT JOIN otherDates 
     ON typeid = auxtypeid 
+0

我們是否可以得到一些樣本起始數據/所需的結果輸出? DB2的哪個版本/平臺?並且是否存在'反射''from_date'函數,無論您的目標格式是什麼(爲什麼不存儲在日期/時間/時間戳類型中)? – 2013-03-26 19:58:38

回答

0
SELECT state, 
COUNT (type) as EQUIPMENT, 
(SELECT COUNT(type) FROM information a LEFT JOIN otherDates on typeid=auxtypeid WHERE a.state=b.state AND (CASE WHEN otherDate > infoDate THEN storedprocedures.dec_to_date(otherDate) ELSE storedprocedures.dec_to_date(infoDate) END) <= (select current date - 5 days from sysibm.sysdummy1) GROUP BY state) as IDLE, 
ROUND((SELECT COUNT(type) FROM information a WHERE a.state=b.state AND (CASE WHEN otherDate > infoDate THEN storedprocedures.dec_to_date(otherDate) ELSE storedprocedures.dec_to_date(infoDate) END) < (select current date - 5 days from sysibm.sysdummy1) GROUP BY state)/ CAST(COUNT(type) as float)*100,1) as percent 
FROM information b 
JOIN validStates ON state=vstate 
LEFT JOIN otherDates ON typeid=auxtypeid 
WHERE state<>'***' 
GROUP BY state 
ORDER BY state; 
0

您的CASE語句是選擇2個字段的最新值的最佳方法。爲什麼不運行查詢?

一旦添加CASE語句...確保您還將CASE語句添加到查詢的GROUP BY部分。

哦...你頭疼的道路之前 - 你可能要考慮與保留關鍵字狀態,類型等作出領域

編輯:哎呀我沒有看到DB2

+0

因此,如果您要插入case case語句,那麼infoDate是如何按case語句分組?我無法按activityDate分組。 – BearSkyview 2013-03-26 17:04:50

+0

如果你可以回答w/SQL與你的case語句失敗,也許我可以幫忙。 我一開始沒有看到DB2。在MS SQL中,您需要通過CASE語句進行分組...在DB2中,您可以簡單地將GROUP BY作爲case語句的別名...就像GROUP BY activityDate – Josh 2013-03-26 17:37:48

0

的標籤你可能有一個更好的時間做類似如下(直到我知道更多關於你的表結構/數據):

WITH Raw as (SELECT a.state, 
        MAX(a.infoDate, COALESCE(c.auxDate, '0001-01-01')) as activityDate 
      FROM information a 
      JOIN validStates b 
       ON b.vstate = a.state 
      LEFT JOIN otherDates c 
        ON typeId = auxtypeid 
      WHERE a.state <> '***'), 
    Total as (SELECT state, COUNT(*) as count 
       FROM Raw 
       GROUP BY state), 
    Idle as (SELECT state, COUNT(*) as count 
       FROM Raw 
       WHERE storedprocedures.to_date(activityDate) < (CURRENT_DATE - 5 DAYS) 
       GROUP BY state) 
SELECT a.state, a.count as EQUIPMENT, b.count as IDLE, 
     ROUND((100.0 * b.count)/a.count, 1) as percent 
FROM Total a 
LEFT JOIN Idle b 
     ON b.state = a.state 
ORDER BY a.state 

... Which'll可能跑得更快了。

請注意,由於使用的LEFT JOIN S,IDLEpercent可以爲null如果給定state沒有「空閒」類型。