2014-03-14 52 views
0

我使用下面的查詢:如何解決ORA-00907:在關係表中括號不

SELECT MONITOR_ALERT_INSTANCE_ID AS ID, 
    description, 
    status, 
    (SELECT COUNT(*) as count 
    FROM monitor_alert_ins_msrmnt 
    WHERE monitor_alert_instance_id IN (SELECT monitor_alert_instance_id 
             FROM monitor_alert_instance 
             WHERE co_mod_asset_id =1234 
            ) 
             GROUP BY monitor_alert_instance_id 
             ORDER BY monitor_alert_instance_id 
) AS COUNT 
FROM monitor_alert_instance 
WHERE co_mod_asset_id = 1234 

內查詢返回正確的結果。我試圖結合兩個查詢一次,並希望得到如下結果:

> ID DESCRIPTION  STATUS COUNT 
> 101  aaa   A  1 
> 102  bbb   B  3 

但它顯示缺少右括號錯誤。請任何幫助。

+0

爲什麼你需要在內部查詢命令? –

回答

1

我沒有看到任何缺失的圓括號,但我確實看到了查詢的危險。您正在查詢中執行一個group by,該查詢可能會生成多行。這將產生一個錯誤,因爲標量子查詢應該只返回一個值。

SELECT MONITOR_ALERT_INSTANCE_ID AS ID, description, status, 
     (SELECT COUNT(*) 
     FROM monitor_alert_ins_msrmnt maim 
     WHERE monitor_alert_instance_id IN (SELECT monitor_alert_instance_id 
              FROM monitor_alert_instance 
              WHERE co_mod_asset_id =1234 
              ) and 
      maim.monitor_alert_instance_id = mai.monitor_alert_instance_id 
     ) AS "COUNT" 
FROM monitor_alert_instance mai 
WHERE co_mod_asset_id = 1234; 

這使得子查詢相關子查詢,除去group byorder by,因爲這些是不需要的。

+0

它完美地工作。謝謝 :-) – Novis