SQL Case 语句子选择内的子选择分组
Posted
技术标签:
【中文标题】SQL Case 语句子选择内的子选择分组【英文标题】:SQL Case statement subselect within a grouped by subselect 【发布时间】:2013-03-26 16:44:35 【问题描述】:我不知道如何修改我的查询以使用 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
【问题讨论】:
我们能否获得一些示例起始数据/所需的结果输出? DB2 的哪个版本/平台?无论您的目标格式是什么(为什么不存储在日期/时间/时间戳类型中),是否有一个“自反”from_date
函数?
【参考方案1】:
您的 CASE 语句是选择 2 个字段的最新值的最佳方式。为什么查询没有运行?
添加 CASE 语句后...确保您也将 CASE 语句添加到查询的 GROUP BY 部分。
哦,...在您头疼之前 - 您可能需要考虑不使用保留关键字(例如状态、类型等)创建字段。
编辑:哎呀我没有看到 DB2 的标签
【讨论】:
因此,如果您要在 infoDate 所在的位置插入 case 语句,您将如何按 case 语句进行分组?我无法按活动日期分组。 如果您可以使用失败的案例语句回复 SQL,也许我可以提供帮助。起初我没有看到 DB2。在 MS SQL 中,您需要按 CASE 语句进行分组……在 DB2 中,也许您可以简单地 GROUP BY 语句的别名……例如 GROUP BY activityDate【参考方案2】: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;
【讨论】:
【参考方案3】:您可能会更好地执行以下操作(直到我对您的表结构/数据了解更多):
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,如果给定的state
没有“空闲”类型,IDLE
和percent
可能为空。
【讨论】:
以上是关于SQL Case 语句子选择内的子选择分组的主要内容,如果未能解决你的问题,请参考以下文章