SQLTeam.com | Weblogs | Forums

Dividing 2 columns into percentages


#1

I'm new to SQL, started yersterday, but have a question.
Below are 2 query's I executed, but now I want to divide type 40 and type 10(highlighted in the text) and then get the percentages like this: (type 40/type10)*100. Please can anyone help me for advice?

SELECT public.session.session_date, COUNT(*) AS article_event_type
FROM public.articleevents, public.session
WHERE public.articleevents.article_event_type = 40 AND public.articleevents.session_ID = public.session.session_ID
GROUP BY public.session.session_date
ORDER BY public.session.session_date

SELECT public.session.session_date, COUNT(*) AS article_event_type
FROM public.articleevents, public.session
WHERE public.articleevents.article_event_type = 10 AND public.articleevents.session_ID = public.session.session_ID
GROUP BY public.session.session_date
ORDER BY public.session.session_date


#2

Something like this perhaps?

with cte as
(
select article_event_type
FROM public.articleevents, 
JOIN public.session 
ON public.articleevents.session_ID = public.session.session_ID
)

select 100*(select count(*) from cte where article_event_type = 40) / nullif((select count(*) from cte where article_event_type = 10), 0) as pct