我有一个像这样的查询,其中我的子查询正在获取多行。
SELECT TOOL.ANUM,
ASSET.ID,
ASSET.DESCRIPTION,
TOOL.DATE,
(SELECT CASE frequnit
WHEN 'WEEKS' THEN finish+(frequency*7)
WHEN 'MONTHS' THEN add_months(finish, pfrequency)
WHEN 'YEARS' THEN add_months(finish, (frequency * 12))
ELSE actfinish+pluscfrequency
END AS frequency
FROM workorder
WHERE worktype = 'CAL'
AND pmnum IS NOT NULL
AND assetnum = '6847'
AND actfinish <
(SELECT actfinish
FROM workorder
WHERE wonum = '2354850')) freq,
FROM tool,
Asset
LEFT JOIN PM ON asset.num = pm.num
AND PM.status = 'ACV'
WHERE asset.anum = tool.anum
AND tool.wo = '2354850'
ORDER BY tool.anum
我的子查询结果是:
SELECT wnum,finish
CASE frequnit
WHEN 'WEEKS' THEN finish+(frequency*7)
WHEN 'MONTHS' THEN add_months(finish, pfrequency)
WHEN 'YEARS' THEN add_months(finish, (frequency * 12))
ELSE actfinish+pluscfrequency
END AS frequency
FROM workorder
WHERE worktype = 'CAL'
AND pmnum IS NOT NULL
AND assetnum = '6847'
AND actfinish <
(SELECT actfinish
FROM workorder
WHERE wonum = '2354850')
wnum finish frequency
10003 12-NOV-15 13-DEC-2015
10004 14-DEC-15 15-DEC-2015
但我需要子查询中max(finish)的freq值。我可以通过如下使用rownum来获得这个:
select * from (subquery order by finish ) where rownum=1;
但是我不应该使用上面的那个,因为我在引用子查询中的主要查询列之一
assetnum ='6847'(assetnum = TOOL.ANUM)
还有其他办法吗? 提前谢谢。
答案 0 :(得分:1)
如果你想要最大值,那么使用它呢?
SELECT . . .
(SELECT MAX(CASE frequnit
WHEN 'WEEKS' THEN finish+(frequency*7)
WHEN 'MONTHS' THEN add_months(finish, pfrequency)
WHEN 'YEARS' THEN add_months(finish, (frequency * 12))
ELSE actfinish+pluscfrequency
END) AS frequency
FROM workorder
WHERE worktype = 'CAL' AND
pmnum IS NOT NULL AND
assetnum = '6847' AND
actfinish < (SELECT actfinish FROM workorder WHERE wonum = '2354850')
) freq,
. . .