我希望此查询只返回一行。我按照它们最有可能发生的顺序放置了CASE语句的WHEN子句,因此最有可能首先评估最可能的TRUE语句。但是,在查询完成之前,似乎每个WHEN语句都被评估,尽管它找到了多个TRUE语句。
SELECT To_Char(T.PRFINISH, 'DD/MM/YY') FINISH,
T.PRNAME
FROM PRTASK T
LEFT OUTER JOIN INV_INVESTMENTS ON T.PRPROJECTID = INV_INVESTMENTS.ID
WHERE T.PRNAME = CASE
WHEN (T.PRNAME = 'Concept Tech PEP Meeting Date' AND T.PRSTATUS != 2) THEN 'Concept Tech PEP Meeting Date'
WHEN (T.PRNAME = 'Concept BRU Meeting Date' AND T.PRSTATUS != 2) THEN 'Concept BRU Meeting Date'
WHEN (T.PRNAME = 'End of Concept Phase' AND T.PRSTATUS != 2) THEN 'End of Concept Phase'
WHEN (T.PRNAME = 'Evaluate Tech PEP Meeting Date' AND T.PRSTATUS != 2) THEN 'Evaluate Tech PEP Meeting Date'
WHEN (T.PRNAME = 'Evaluate BRU Meeting Date' AND T.PRSTATUS != 2) THEN 'Evaluate BRU Meeting Date'
WHEN (T.PRNAME = 'Evaluate TSC Meeting Date' AND T.PRSTATUS != 2) THEN 'Evaluate TSC Meeting Date'
WHEN (T.PRNAME = 'End of Evaluate Phase' AND T.PRSTATUS != 2) THEN 'End of Evaluate Phase'
WHEN (T.PRNAME = 'End of Analyse Phase' AND T.PRSTATUS != 2) THEN 'End of Analyse Phase'
WHEN (T.PRNAME = 'End of Design Phase' AND T.PRSTATUS != 2) THEN 'End of Design Phase'
WHEN (T.PRNAME = 'End of Build Phase' AND T.PRSTATUS != 2) THEN 'End of Build Phase'
WHEN (T.PRNAME = 'End of Test Phase' AND T.PRSTATUS != 2) THEN 'End of Test Phase'
WHEN (T.PRNAME = 'In Service' AND T.PRSTATUS != 2) THEN 'In Service'
WHEN (T.PRNAME = 'End of Implement Phase' AND T.PRSTATUS != 2) THEN 'End of Implement Phase'
WHEN (T.PRNAME = 'End of Closure Phase' AND T.PRSTATUS != 2) THEN 'End of Closure Phase'
ELSE 'In Service'
END
AND INV_INVESTMENTS.CODE = '007058'
即使第一个WHEN子句为真,也会返回多行。我认为CASE语句在找到TRUE语句时就完成了。
编辑:也许我不是很好解释,让我们试试这个:
INV_INVESTMENTS表中的每个ID都是一个项目。每个项目在PRTASK表中都有一组任务。在这个PRTASK表中,我们将有任务名称(PRNAME),状态(PRSTATUS)和完成日期(PRFINISH)。任务列表按其在项目中的顺序排列:
1。 Concept Tech PEP会议日期
的 2。概念BRU会议日期
第3。概念结束阶段
的 4。评估技术PEP会议日期
的 5。评估BRU会议日期
的 6。评估TSC会议日期
其余任务按照我在CASE语句中列出的顺序排列。我希望此查询返回的是,PRSTATUS为0或1的列表中第一个任务的完成日期。
答案 0 :(得分:1)
试试这个。您可以将已知名称放入表中,然后在连接中使用它。这将简化您对以下内容的选择:
SELECT TOP 1 To_Char(T.PRFINISH, 'DD/MM/YY') FINISH,
T.PRNAME
FROM PRTASK T
LEFT OUTER JOIN INV_INVESTMENTS ON T.PRPROJECTID = INV_INVESTMENTS.ID
JOIN (
SELECT 'Concept Tech PEP Meeting Date' as Name, 1 as [Order]
UNION All
SELECT '<your next PRNAME here', 2 as [Order]
UNION ALL ...
) names on names.Name = T.PRNAME
where PRSTATUS IN (0,1) AND PRINV_INVESTMENTS.CODE = '007058'
ORDER BY names.Order
答案 1 :(得分:0)
我仍然不明白你想要做什么(没有给出样本数据/结果),但我会像这样重组它:
SELECT To_Char(PRFINISH, 'DD/MM/YY') AS FINISH, PRNAME
FROM (
SELECT MAX(PRFINISH),
PRNAME
FROM PRTASK AS T
WHERE (PRNAME = 'In Service' OR PRSTATUS != 2)
AND PRPROJECTID IN
(SELECT ID
FROM INV_INVESTMENTS
WHERE CODE = '007058' )
GROUP BY PRNAME
) AS TABLE1
注意,这是假设您在查询中列出了PRNAME
的所有可能值。