Reputation: 11
I've been stuck at this but my end goal is to get the % of negative, %positive, and % neutral for the overall data and group by dates (daily) as well as the categories. Thank you.
Upvotes: 1
Views: 1066
Reputation: 1269943
Just use window functions:
select mlsentimentzone,
(count(*) * 1.0 / sum(count(*)) over ()) as ratio
from t
group by mlsentimentzone;
Or, if you want this by date, use conditional aggregation:
select date,
avg(case when mlsentimentzone = 'negative' then 1.0 else 0.0 end) as negative,
avg(case when mlsentimentzone = 'neutral' then 1.0 else 0.0 end) as neutral,
avg(case when mlsentimentzone = 'positive' then 1.0 else 0.0 end) as positive
from t
group by date
order by date;
Upvotes: 4