※ 引述《newbiegg (威爾)》之銘言:
: 某表格如下, 紀錄顧客的每筆交易
: Customer Amount Date
: 1 $10 10/1/2014
: 2 $15 11/1/2014
: 1 $20 11/2/2014
: 3 $5 11/8/2014
: :
: :
: 請問如何計算每位顧客"過去 10 筆"交易平均? 結果如下:
: Customer Average
: 1 $9.5
: 2 $12
: 3 $8
: :
: 當然, 有些顧客可能全部只有一筆. 有些上百筆. 但只取最後 10 筆平均.
: 謝謝
with tb as(
select
row_number() over(partition by Customer order by Customer,Date desc) as row_id,
* from table)
select Customer,avg(Amount) as Average
From tb
Where row_id<11
group by Customer