如果我有以下的表稱爲stats
:計算總和,平均和DB 90%/ 2
type duration
------ ---------
get 10
get 15
get 20
put 12
put 14
put 16
我知道我能得到sum()
和average()
用類似的查詢:
select
type,
sum(duration) as "total duration",
avg(duration) as "average duration"
from
stats
group by
type
而且我還可以使用窗口函數功能獲得90%的持續時間和max()
:
select
type,
avg("dur") as "average duration of the fastest 90%",
max("dur") as "max duration of the fastest 90%"
from
(
select
type,
duration as "dur",
row_number() over (partition by type order by duration asc) as "seqnum"
count(*) over (partition by type) as "totnum"
from
stats
)
where
"seqnum" <= (.9 * "totnum")
group by
type
但我很努力地瞭解如何將兩者合併,以便我可以有一個查詢返回:type
,total duration
,average duration
,average duration of the fastest 90%
,max duration of the fastest 90%
?
因此,'seqnum「<(0.9 *」totnum「)時的情況」then「dur」end「意思是」如果符合標準,那麼在聚合函數中使用它,否則不要使用它? –
@MattFelzani。 。 。基本上。它實際上意味着評估表達式; 'else'子句是'NULL'。大多數聚合函數忽略NULL值。 –