SQL Case语句subselect在按subselect分组

时间:2013-03-26 16:44:36

标签: sql db2 case sql-subselect

我无法弄清楚如何修改我的查询以使用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

3 个答案:

答案 0 :(得分:0)

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

添加CASE语句后...确保您还将CASE语句添加到查询的GROUP BY部分。

哦,......在你遇到麻烦之前 - 你可能要考虑制作带有保留关键字的字段,例如州,类型等。

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

答案 1 :(得分: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 JOIN s,如果给定的IDLE没有“空闲”类型,则percentstate可能为空。

答案 2 :(得分: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;