I have this SQL select and I don't know how can I use 'total' to do the division.
select count(*) as total
from table
where id = 15/total;
I get this error:
ORA-00904: "TOTAL": invalid identifier
I have this SQL select and I don't know how can I use 'total' to do the division.
select count(*) as total
from table
where id = 15/total;
I get this error:
ORA-00904: "TOTAL": invalid identifier
use a subquery and analytic function
select * from
(
select id, count(*) over() as total
from table
) s
where id=15/total
UPDATE based on your comment - finding percent for each age group:
select age_group, count(*)*100/total as pct
from
(
select
case when age between 40 and 45 then 'group_45'
when age between 20 and 30 then 'group_30'
... add more groups
else 'other'
end as age_group,
count(*) over() as total
)s
group by age_group
jarih is right you can't use an alias like that, your one alternative is:
WITH TOTALS AS (
SELECT COUNT(*) AS TOTAL FROM TABLE
) SELECT COUNT(*) FROM TABLE CROSS JOIN TOTALS
WHERE TABLE.ID = 15/TOTAL;