I thought this would be a slam-dunk, but apparently not. I have a SELECT COUNT(id) FROM {table X}, which functions properly.
I then want to divide the results of that COUNT() by the results of another SELECT COUNT() using the “/” operator, but I get a syntax error on that “/”
The exact expression I’m trying to evaluate is below. I want to divide the number of id values in the members table by the number of id values in the orchestras table. The individual SELECT statements function properly, but the “/” is apparently a problem, and I can’t determine why.
Has anyone seen something like this, and/or does someone have any ideas about what to try? I’m really at the end of my proverbial rope.
SELECT COUNT(id) FROM members / SELECT COUNT(id) FROM orchestras
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Hi there,
The issue you’re facing is related to your SQL syntax.
Basically in SQL, you can’t directly divide the results of two
SELECT COUNT()
statements using the/
operator like that.Instead, you need to use subqueries or
CROSS JOIN
to combine the results of the twoCOUNT()
queries and then perform the division.Here’s how you can do it using a subquery:
You can use subqueries to first compute each
COUNT()
and then divide the results:Alternatively, you can use a
CROSS JOIN
to calculate the ratio in a single query:SELECT COUNT()
is computed separately, and then the results are divided.CROSS JOIN
combines the results of bothCOUNT()
queries into a single row, where you can perform the division.Make sure that the
COUNT(id)
in both tables doesn’t result in zero to avoid a division by zero error.These approaches should resolve the syntax issue and allow you to calculate the ratio of the counts from the two tables.
- Bobby