1

次の PL/SQL クエリがあります。

SELECT customer_id, table_id, count(table_id) as reserved
FROM { derived table }
GROUP BY customer_id,table_id
ORDER BY reserved DESC

私はこの結果を持っています:

http://i.stack.imgur.com/ATfUw.png

だから今、最初の2行(列に応じた最大値)を取得したいので、次のreservedクエリを試しました:

SELECT customer_id,table_id,max(reserved) from
(
    SELECT customer_id, table_id, count(table_id) as reserved
    FROM { derived table }
    GROUP BY customer_id,table_id
    ORDER BY reserved DESC
)
GROUP BY customer_id, table_id

上記と同じ結果を受け取りました...

注:結果は単なる例であり、次回は最大値を持つ3、1、またはそれ以上の行があるかもしれません

4

3 に答える 3

0

あなたのクエリは非常に近いです。外側のクエリは、集計をやり直すのではなく、2 つの行を選択する必要があります。

SELECT customer_id, table_id, reserved from
(
    SELECT customer_id, table_id, count(table_id) as reserved
    GROUP BY customer_id,table_id
    ORDER BY reserved DESC
)
where rownum <= 2;
于 2013-05-29T19:48:51.407 に答える