Answer the question
In order to leave comments, you need to log in
How to count multiple count() in select section using as notation?
Hello.
For example, I want to calculate the percentage of selected fields through count(), but sql swears that count_id1 and count_id2 are unknown , although I designate them through as count_id1 and as count_id2
select
...
count(table.id1) as count_id1,
count(table.id2) as count_id2,
round(sum(count_id1) / sum(count_id2) * 100, 1) as percent
from table
...
Answer the question
In order to leave comments, you need to log in
SELECT t1.*,
round(sum(count_id1) / sum(count_id2) * 100, 1) as percent
FROM
(
select
...
count(table.id1) as count_id1,
count(table.id2) as count_id2
from table
...
) AS t1
You cannot refer to these numbers, because you are naming the resulting column, not creating a variable.
...
count(table.id1) as count_id1,
count(table.id2) as count_id2,
...
SELECT round((SELECT COUNT(table.id1) FROM table) / (SELECT COUNT(table.id2) FROM table) * 100, 1) as percent
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question