'SQL:How use analytic function on multiple columns in Oracle?
I need to statistics multiple columns,the following is my writing:
with
t_suml1 as(select id, date, sum(list1) result from table group by id,date),
t_avgl2 as(select id, date, avg(list2) result from talbe group by id,date),
t_countl3 as(select id, date, count(list3) result from table group by id,date)
select s.id, s.date, s.result, t_avgl2.result, t_countl3.result
from t_suml1 s
inner join t_avgl2 a on s.id=a.id and s.date=a.date
inner join t_countl3 c on s.id=c.id and s.date=c.date
Is there another way to do this?
Solution 1:[1]
select id, date,sum(list1),avg(list2), count(list3) result from table group by id,date
Solution 2:[2]
select s.id, s.date, s.result, t_avgl2.result
,sum(list1) over (partition by id, date)
,avg(list1) over (partition by id, date)
,count(list1) over (partition by id, date)
--totals:
,sum(list1) over () as total_sum
,avg(list1) over () as total_avg
,count(list1) over () as total_cnt
from t_suml1 s
inner join t_avgl2 a on s.id=a.id and s.date=a.date
inner join t_countl3 c on s.id=c.id and s.date=c.date
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | vinzee |
Solution 2 | are |