0

我不知道如何修改我的查询以使用 information.infoDate 和 otherDates.auxDate 之间的最新日期。

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
4

3 回答 3

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

......这也可能会跑得更快。

请注意,由于使用了LEFT JOINs,IDLE并且如果给定没有“空闲”类型,percent则可能为 null 。state

于 2013-03-26T20:23:13.137 回答
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;
于 2013-03-26T20:47:56.377 回答
0

您的 CASE 语句是选择 2 个字段的最新值的最佳方式。为什么查询没有运行?

一旦你添加了 CASE 语句......确保你也将 CASE 语句添加到查询的 GROUP BY 部分。

哦,...在您头疼之前-您可能要考虑不要使用保留关键字(例如状态,类型等)创建字段。

编辑:哎呀我没有看到 DB2 的标签

于 2013-03-26T16:51:23.823 回答