私は一般的にSQLの経験が豊富ではなく、かなり具体的なタスクを達成しようとしています-最初にクエリを実行して、ヒット数が上位のすべてのユニットのIDを取得し、次にその実行から特定の期間におけるこれらの ID のすべてのタイプのヒットのメッセージとカウントを再度取得します。最初のクエリについては、次のようになります。
SELECT entity, count(entity) as Count
from plugin_status_alerts
where entered BETWEEN now() - INTERVAL '14 days' AND now()
group by entity
order by count(entity) DESC
limit 10
これにより、次の結果が返されます。
"38792";3
"39416";2
"37796";2
"39145";2
"37713";2
"37360";2
"37724";2
"39152";2
"39937";2
"39667";2
次に、その結果セットを使用して、エンティティと status_code で並べ替える別のクエリを実行します。私はこのようなことを試しました:
SELECT status_code, entity, COUNT(status_code) statusCount
FROM plugin_status_alerts
where updated BETWEEN now() - INTERVAL '14 days' AND now() AND entity IN
(SELECT id.entity, count(id.entity) as Count
from plugin_status_alerts id
where id.updated BETWEEN now() - INTERVAL '14 days' AND now()
group by id.entity
order by count(id.entity) DESC
limit 10
)
GROUP BY status_code, entity
しかし、私はエラーが発生します
ERROR: subquery has too many columns
これが私が進むべきルートなのか、それとも自己結合を試みる必要があるのか 、どちらにしても、現在起こっていることを修正する方法がわからない.