I should note that id
is not unique in the tdemog_pfp table - there is a unique field there that auto-increments - could this be an issue?
Ok, I have the following SQL query:
SELECT id, name, total_staff AS StaffCount, COUNT( q60a ) AS TotalResp, (COUNT( q60a ) / total_staff * 100) AS Perc
FROM tdemog_pfp
LEFT JOIN tresults_pfp ON tdemog_pfp.id = tresults_pfp.q60a
GROUP BY name
ORDER BY name
Now, the demog table has multiple rows where entries can have q59, q60a, q60b and so on for the colomn qnum
I want to bring back the results WHERE qnum = q60a
but I have a problem, when I add it to the above to make the following - it brings back zero results (an empty result set), which is incorrect:
SELECT id, name, total_staff AS StaffCount, COUNT( q60a ) AS TotalResp, (COUNT( q60a ) / total_staff * 100) AS Perc
FROM tdemog_pfp
LEFT JOIN tresults_pfp ON tdemog_pfp.id = tresults_pfp.q60a
WHERE qnum = q60a
GROUP BY name
ORDER BY name
Where am I going wrong?
Thanks,
H.