0

我有以下查询(其中的一部分),我想要的是首先为 LTV 制作存储桶,例如 0-0.2;0.2-0.4;0.4-0.6。然后在这些桶中,我想将它们分成较小的桶,供 Crate 使用,例如 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
4

1 回答 1

1

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
于 2013-05-31T12:43:11.387 回答