我正在使用以下查询:
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 by
和order by
,因为不需要这些子查询。