首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于存储桶sql的聚合

基于存储桶sql的聚合
EN

Stack Overflow用户
提问于 2013-05-31 20:33:10
回答 1查看 319关注 0票数 0

我有下面的查询(它的一部分),我想要的是首先为LTV制作桶,像0-0.2;0.2-0.4;0.4-0.6。然后在这些存储桶中,我想将它们划分为更小的存储桶,如2-4;4-6;6-8。所以总共有9个桶。

对于这些桶,我想取SavRate和SavIncentive的平均值,以及PartialPrepay和OutNot的和。

我该怎么做呢,非常感谢

代码语言:javascript
复制
   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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-31 20:43:11

您可以使用case语句定义存储桶。然后您可以按存储桶进行聚合:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16857387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档