0

計算/レポートの目的で次のクエリを試していますが、エラーが発生しています。

SELECT c.idx, c.club_name, s.sale_event_date, 
COUNT(s.*) AS total_guests, 
(SELECT COUNT(s.*) FROM sales s WHERE t2.qr_scanned = 1 AND t2.sale_event_date = s.sale_event_date) AS total_scanned_guests, 
SUM(s.rep_sale_commission) AS total_rep_commission, 
SUM(s.sale_commission) AS total_admin_fees
FROM club c
LEFT JOIN sales s ON s.club_id = c.idx
WHERE c.admin_id = 37
GROUP BY s.sale_event_date
ORDER BY sale_event_date DESC

エラーは次のとおりです。

use near '*) AS total_guests, (SELECT COUNT(s.id) FROM sales s WHERE t2.qr_scanned = 1 AN' at line 2
4

2 に答える 2

1

にテーブル エイリアスを含めませんcount(*)

この問題を修正すると、サブクエリにエイリアスの問題があることがわかります。サブクエリのエイリアスは次のようにする必要があると思いt2ますs:

SELECT c.idx, c.club_name, s.sale_event_date, COUNT(*) AS total_guests, 
       (SELECT COUNT(*)
        FROM sales t2
        WHERE t2.qr_scanned = 1 AND t2.sale_event_date = s.sale_event_date
       ) AS total_scanned_guests, 
       SUM(s.rep_sale_commission) AS total_rep_commission, 
       SUM(s.sale_commission) AS total_admin_fees
FROM club c LEFT JOIN
     sales s ON s.club_id = c.idx
WHERE c.admin_id = 37
GROUP BY s.sale_event_date
ORDER BY sale_event_date DESC;

実際、サブクエリはまったく必要ないと思います。条件付き集計が必要なようです。

SELECT c.idx, c.club_name, s.sale_event_date, COUNT(sales.club_id) AS total_guests, 
       sum(s.qr_scanned = 1) AS total_scanned_guests, 
       SUM(s.rep_sale_commission) AS total_rep_commission, 
       SUM(s.sale_commission) AS total_admin_fees
FROM club c LEFT JOIN
     sales s ON s.club_id = c.idx
WHERE c.admin_id = 37
GROUP BY s.sale_event_date
ORDER BY sale_event_date DESC;
于 2013-08-24T19:18:20.683 に答える
0

変化する

COUNT(s*) AS total_guests

COUNT(*) AS total_guests

または

COUNT(s.id) AS total_guests

少なくともそれはあなたの差し迫った問題のようです。

于 2013-08-24T19:17:09.383 に答える