Question

I want to select the number of people who have signed up for an activity, which is given by the signup_limit, stored in activity_info, and the number of people who have signed up for it, stored in signups. I want it to calculate with the number of signups as zero if there is no one signed up, but as i have it, it instead drops the row entirely. I tried to use a left outer join, but I get the same result. Any suggestions?

SELECT info.code, info.signup_limit - COUNT(*) AS swag
     FROM activity_info AS info 
         LEFT OUTER JOIN signups ON info.code = signups.activity_code, variables
WHERE 
    info.code IN (
      SELECT schedule.code FROM schedule,variables WHERE schedule.date = variables.week
    ) AND variables.week = signups.date
GROUP BY signups.activity_code

Update: Resolved the issue thanks to the comments. Thanks everyone!

Was it helpful?

Solution

I guess this is what you want:

SELECT 
    AI.code, 
    (
        AI.signup_limit - 
        (
            SELECT 
                COUNT(*)
            FROM 
                signups AS S
            WHERE 
                S.activity_code = AI.code
        )
    ) AS swag
FROM 
    activity_info AS AI
WHERE
    info.code IN 
    (
        SELECT
            schedule.code 
        FROM 
            schedule INNER JOIN variables ON schedule.date = variables.week
    ) AND 
    variables.week = signups.date

OTHER TIPS

There is a word in the middle of the SQL variables. Remove to stay well:

SELECT info.code,
       info.signup_limit - count(*) AS swag
FROM activity_info AS info
LEFT OUTER JOIN signups ON info.code = signups.activity_code
WHERE info.code IN
    (SELECT schedule.code
     FROM schedule
     INNER JOIN variables ON schedule.date = variables.week)
  AND variables.week = signups.date
GROUP BY signups.activity_code
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top