0

I am trying the following query for calculation/reporting purposes, but it is giving an error.

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

The error is:

use near '*) AS total_guests, (SELECT COUNT(s.id) FROM sales s WHERE t2.qr_scanned = 1 AN' at line 2
1
  • @iBlue Question updated! Commented Aug 24, 2013 at 19:16

2 Answers 2

1

You don't include the table alias in count(*).

Once you fix that problem, you'll see that you have an alias problem in the subquery. I think the subquery alias should be t2 rather than 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;

In fact, I don't think you need the subquery at all. It looks like you just want conditional aggregation:

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;
Sign up to request clarification or add additional context in comments.

5 Comments

its working perfect but last row results are... New Event | NULL | 7 | NULL | NULL | NULL
@seoppc . . . You are using a left outer join, so you are getting all values from club even when there are no matching results in sales. That might explain this problem.
the above result is fine because there is not row in sales table for this id, but why total_guests are showing 7 while there are not results in second table.. Please help
I think there is some problem with COUNT(*) AS total_guests
@seoppc . . . I don't fully understand the data structure. Perhaps you want to count the ones that match in the sales table.
0

Change

COUNT(s*) AS total_guests

to

COUNT(*) AS total_guests

or to

COUNT(s.id) AS total_guests

At least that seems to be your immediate problem.

1 Comment

No, It was typo, i have updated the question, and still showing same error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.