如何将 CASE WHEN 语句改进为同一行上的 return 结果
How to improve CASE WHEN statement to return result on same line
目前正在处理来自 OpenEdge 进度数据库的一些 SQL 脚本。目前我正在尝试 return 根据事件是 Estimated
还是 Actual
计时。这可以通过列中的结果来识别。我得到的问题是数据的工作方式是它显示相同的 EveNumber
但在两行上(一条用于估计,一条用于实际)。我想将这些行合二为一。
我已经尝试了下面的代码,但这让我得到的结果仍然是两行。我尝试将 case when 也放入 Else
部分,但这没有用。下面是我的浓缩代码:
SELECT
Event_0.EveNumber
, (CASE WHEN Object_0.ObjName = '818 Artwork typeset duration' THEN TemplateRunObject_0.TroValue ELSE NULL END) AS 'Actual'
, (CASE WHEN Object_0.ObjName = '818 Artwork Estimated typeset duration' THEN TemplateRunObject_0.TroValue ELSE NULL END) AS 'Estimated'
FROM
SBS.PUB.Event Event_0
LEFT JOIN SBS.PUB.TemplateRunObject TemplateRunObject_0 ON Event_0.TemplateRunID = TemplateRunObject_0.TemplateRunID
JOIN SBS.PUB.Object Object_0 ON TemplateRunObject_0.ObjectId = Object_0.ObjectId
原始数据布局
Evenumber Ogjname TroValue
123 818 Artwork typeset duration 15
123 818 Artwork Estimated typeset duration 30
目前的结果是这样的:
EveNumber Actual Estimated
123 15 0
123 0 30
我希望他们如何出现
EveNumber Actual Estimated
123 15 30
使用max()
并按
分组
SELECT
Event_0.EveNumber
, max((CASE WHEN Object_0.ObjName = '818 Artwork typeset duration' THEN TemplateRunObject_0.TroValue ELSE NULL END) ) AS 'Actual'
,max( (CASE WHEN Object_0.ObjName = '818 Artwork Estimated typeset duration' THEN TemplateRunObject_0.TroValue ELSE NULL END) )AS 'Estimated'
FROM
SBS.PUB.Event Event_0
LEFT JOIN SBS.PUB.TemplateRunObject TemplateRunObject_0 ON Event_0.TemplateRunID = TemplateRunObject_0.TemplateRunID
JOIN SBS.PUB.Object Object_0 ON TemplateRunObject_0.ObjectId = Object_0.ObjectId
group by Event_0.EveNumber
对你有帮助;最大()函数。但是你应该忘记 GROUP BY。
SELECT Event_0.EveNumber
, (MAX(CASE WHEN Object_0.ObjName = '818 Artwork typeset duration' THEN TemplateRunObject_0.TroValue ELSE NULL END)) AS 'Actual'
, (MAX(CASE WHEN Object_0.ObjName = '818 Artwork Estimated typeset duration' THEN TemplateRunObject_0.TroValue ELSE NULL END)) AS 'Estimated'
FROM
SBS.PUB.Event Event_0
LEFT JOIN SBS.PUB.TemplateRunObject TemplateRunObject_0 ON Event_0.TemplateRunID = TemplateRunObject_0.TemplateRunID
JOIN SBS.PUB.Object Object_0 ON TemplateRunObject_0.ObjectId = Object_0.ObjectId
GROUP BY Event_0.EveNumber
目前正在处理来自 OpenEdge 进度数据库的一些 SQL 脚本。目前我正在尝试 return 根据事件是 Estimated
还是 Actual
计时。这可以通过列中的结果来识别。我得到的问题是数据的工作方式是它显示相同的 EveNumber
但在两行上(一条用于估计,一条用于实际)。我想将这些行合二为一。
我已经尝试了下面的代码,但这让我得到的结果仍然是两行。我尝试将 case when 也放入 Else
部分,但这没有用。下面是我的浓缩代码:
SELECT
Event_0.EveNumber
, (CASE WHEN Object_0.ObjName = '818 Artwork typeset duration' THEN TemplateRunObject_0.TroValue ELSE NULL END) AS 'Actual'
, (CASE WHEN Object_0.ObjName = '818 Artwork Estimated typeset duration' THEN TemplateRunObject_0.TroValue ELSE NULL END) AS 'Estimated'
FROM
SBS.PUB.Event Event_0
LEFT JOIN SBS.PUB.TemplateRunObject TemplateRunObject_0 ON Event_0.TemplateRunID = TemplateRunObject_0.TemplateRunID
JOIN SBS.PUB.Object Object_0 ON TemplateRunObject_0.ObjectId = Object_0.ObjectId
原始数据布局
Evenumber Ogjname TroValue
123 818 Artwork typeset duration 15
123 818 Artwork Estimated typeset duration 30
目前的结果是这样的:
EveNumber Actual Estimated
123 15 0
123 0 30
我希望他们如何出现
EveNumber Actual Estimated
123 15 30
使用max()
并按
SELECT
Event_0.EveNumber
, max((CASE WHEN Object_0.ObjName = '818 Artwork typeset duration' THEN TemplateRunObject_0.TroValue ELSE NULL END) ) AS 'Actual'
,max( (CASE WHEN Object_0.ObjName = '818 Artwork Estimated typeset duration' THEN TemplateRunObject_0.TroValue ELSE NULL END) )AS 'Estimated'
FROM
SBS.PUB.Event Event_0
LEFT JOIN SBS.PUB.TemplateRunObject TemplateRunObject_0 ON Event_0.TemplateRunID = TemplateRunObject_0.TemplateRunID
JOIN SBS.PUB.Object Object_0 ON TemplateRunObject_0.ObjectId = Object_0.ObjectId
group by Event_0.EveNumber
对你有帮助;最大()函数。但是你应该忘记 GROUP BY。
SELECT Event_0.EveNumber
, (MAX(CASE WHEN Object_0.ObjName = '818 Artwork typeset duration' THEN TemplateRunObject_0.TroValue ELSE NULL END)) AS 'Actual'
, (MAX(CASE WHEN Object_0.ObjName = '818 Artwork Estimated typeset duration' THEN TemplateRunObject_0.TroValue ELSE NULL END)) AS 'Estimated'
FROM
SBS.PUB.Event Event_0
LEFT JOIN SBS.PUB.TemplateRunObject TemplateRunObject_0 ON Event_0.TemplateRunID = TemplateRunObject_0.TemplateRunID
JOIN SBS.PUB.Object Object_0 ON TemplateRunObject_0.ObjectId = Object_0.ObjectId
GROUP BY Event_0.EveNumber