0
我們正在根據Telco爲Call Detail Records(CDR)創建一個白天的聚合。總計在CREDIT_CHARGED上,這不是問題。但是,我們希望在相同的查詢中包含用戶所在的最新價格計劃。隨着訂戶在一天內更改價格計劃,數據中會出現一天中有兩種不同價格計劃的情況。不知怎的,也有在那裏我找到兩個記錄有不同的價格計劃的條目,同時數據的瞬間:Postgres:使用MAX和SUM聚合sql返回兩行而不是一個
MID ACC SUB CALL_TIME SUM_CREDIT
xx8567773 TSBS 0 2014-10-01 10:03:38 10499
xx8567773 TS 0 2014-10-01 10:03:38 1426
這打破了我的聚集SQL,它應該用於MID xx8567773有11925只返回一個行SUM_CREDIT
下面是我使用了SQL:
select t2.mid, t2.acc, t2.sub, t2.call_time, t2.sum_credit
from
(select t.mid, t.acc, t.sub, t.call_time,
sum(t.credit) over(partition by t.mid, date(t.call_time), t.acc, t.sub) as sum_credit,
max(call_time) over(partition by t.mid, t.acc, t.sub) as time_
from (
select calling_isdn as mid, acc_profile as acc, subscriber_type as sub, call_sta_time as call_time,
credit_charged as credit
from vms_cdricc
where date(call_sta_time) = '2014-10-01'
and credit_charged > 0
and calling_isdn = 'xx8567773'
) as t
) as t2
where t2.call_time = t2.time_
如何用上述SQL改爲只返回一行(我想在這種情況下,很難說哪個記錄是年輕或年長,所以它必須是隨機的),當有兩個時記錄在同一時間發生(誰說數據總是乾淨?)。
在此先感謝您的幫助和建議!