2016-08-04 51 views
-1

我需要統計多列,以下是我寫的字:SQL:如何在Oracle的多列上使用分析函數?

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 

是否有另一種方式做到這一點?

+1

爲什麼不使用select ID,日期,金額(列表1),AVG(列表2),計數(項目list3)由ID,日期表組的結果?你有3個不同的表格? – adam

回答

0

對不起,這是一個愚蠢的問題。

select id, date,sum(list1),avg(list2), count(list3) result from table group by id,date 
0

Oracle Analytic functions

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