S
S
SvizzZzy2018-08-06 12:22:22
MySQL
SvizzZzy, 2018-08-06 12:22:22

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

2 answer(s)
M
mletov, 2018-08-06
@SvizzZzy

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

N
Nikita Zotov, 2018-08-06
@MrSalivan

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,
...

Here is a working request:
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 question

Ask a Question

731 491 924 answers to any question