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 回答