簡體   English   中英

基於存儲桶SQL聚合

[英]Aggregating based on buckets sql

我有以下查詢(它的一部分),我想要的是首先為LTV制作存儲桶,例如0-0.2; 0.2-0.4; 0.4-0.6。 然后在這些存儲桶中,我想將它們分成較小的存儲桶,用於2-4個板條箱; 4-6; 6-8。 所以總共有9個桶

對於這些存儲桶,我想取SavRate和SavIncentive的平均值以及PartialPrepay和OutNot的總和。

我該怎么做,非常感謝

   LTV             CRate    SavRate SavIncentive    PartialPrepay   OutNot
   0.6684459906     5,5     4,5     0,4              0           26,81
   0.1329765857     5,1     3       2,5              28          77,2
   0.212585034      6,8     4,5     2,3              17981       22689,01
   0.6613789002     4,3     3,2     1,1              17          37,04
   0.4251691757     6,3     3       3,3              99          121,09
   0.1774713259     4,9     3       1,9              0           63

您可以使用case語句定義存儲桶。 然后,您可以按存儲桶進行匯總:

select ltvBucket, CrateBucket,
       avg(SavRate), avg(SavIncentive), sum(PartialPrepay)
from (select t.*,
             (case when ltv between 0.0 and 0.2 then 'LTV:0.0-0.2'
                   when ltv between 0.2 and 0.4 then 'LTV:0.2-0.4'
                   when ltv between 0.4 and 0.6 then 'LTV:0.4-0.6'
                   when ltv between 0.6 and 0.8 then 'LTV:0.6-0.8'
                   else 'LTV:other'
             end) as ltvbucket,
            (case when Crate between 2 and 4 then 'Crate:2-4'
                  when Crate between 4 and 6 then 'Crate:4-6'
                  when Crate between 6 and 8 then 'Crate:6-8'
                  else 'Crate:Other'
            end) as CrateBucket
      from t
     ) t
group by ltvBucket, CrateBucket

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM