0

このクエリが失敗する理由がわかりません。group by 句には、select にある列がありますが、まだエラーが発生しています。どうしてこれなの?以下は私の質問です。

SELECT c.customer_first_name, c.customer_last_name, MAX(SUM(cost_line))
FROM customers c LEFT OUTER JOIN orders o USING(customer_numb)
LEFT OUTER JOIN order_lines l USING (order_numb)
GROUP BY c.customer_first_name, c.customer_last_name
ORDER BY customer_numb;

このエラーが発生する

SQL Error: ORA-00937: not a single-group group function
00937. 00000 -  "not a single-group group function"
4

3 に答える 3

2

mti2935 が示唆するように、GROUP BY の l.cost_line を取り除く必要があり、max() 関数も取り除く必要があります。このように複数の集計関数を使用することはできません。

SELECT c.customer_first_name, c.customer_last_name, SUM(cost_line)
FROM customers c LEFT OUTER JOIN orders o USING(customer_numb)
LEFT OUTER JOIN order_lines l USING (order_numb)
GROUP BY c.customer_first_name, c.customer_last_name
ORDER BY customer_numb;

http://sqlfiddle.com/#!2/fdbba1/6

于 2013-11-04T21:03:25.867 に答える
0

SELECT 句の集計関数でこのフィールドを使用しているため、GROUP BY 句に l.cost_line を含めないでください。GROUP BY 句に l.cost_line を指定せずに試して、問題が解決するかどうかを確認してください。

于 2013-11-04T20:58:15.667 に答える
0

最大値が必要な場合は、サブクエリですべてラップする必要があります。次をご覧ください。

SELECT c.customer_first_name, c.customer_last_name, SUM(cost_line)
FROM customers c LEFT OUTER JOIN orders o USING(customer_numb)
LEFT OUTER JOIN order_lines l USING (order_numb)
GROUP BY c.customer_first_name, c.customer_last_name
HAVING SUM(cost_line) = (
  SELECT MAX(sum_cost_line)
    FROM
    (SELECT SUM(cost_line) sum_cost_line
      FROM customers c LEFT OUTER JOIN orders o USING(customer_numb)
        LEFT OUTER JOIN order_lines l USING (order_numb)
      GROUP BY c.customer_first_name, c.customer_last_name) a
  )
ORDER BY customer_numb;

Oracleを使用している場合は編集してください。より簡単にすることができます:

SELECT c.customer_first_name, c.customer_last_name, SUM(cost_line)
    FROM customers c LEFT OUTER JOIN orders o USING(customer_numb)
    LEFT OUTER JOIN order_lines l USING (order_numb)
    GROUP BY c.customer_first_name, c.customer_last_name
    HAVING SUM(cost_line) = (
      SELECT MAX(SUM(cost_line))
          FROM customers c LEFT OUTER JOIN orders o USING(customer_numb)
            LEFT OUTER JOIN order_lines l USING (order_numb)
          GROUP BY c.customer_first_name, c.customer_last_name
      )
    ORDER BY customer_numb;
于 2013-11-04T21:05:16.260 に答える