V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
hisune
V2EX  ›  问与答

求教大佬,这种统计 sql 如何写?

  •  
  •   hisune · 2018-09-29 17:53:53 +08:00 · 1248 次点击
    这是一个创建于 2029 天前的主题,其中的信息可能已经有所发展或是发生改变。
    金额|账号数量
    ---|---
    2000~4999|
    5000~9999|

    就是从订单的流水数据中统计每个充值区间段的用户 id 数量有多少,假定数据库表结构是这样的:
    money
    uid
    6 条回复    2018-09-29 18:11:26 +08:00
    liprais
        1
    liprais  
       2018-09-29 17:56:03 +08:00
    充值金额改成区间不就行了

    case when money > 2000 and money <= 4999 then 'a'
    when your-condition-here then 'b'
    else
    end
    这样
    然后 group by count(*) 就行了
    hisune
        2
    hisune  
    OP
       2018-09-29 17:58:22 +08:00
    @liprais 你这个是订单金额是某个区间的数量吧?其实我要的是用户累计订单金额落在区间的用户数量
    GTim
        3
    GTim  
       2018-09-29 17:59:50 +08:00
    每个段的大小不一样?
    liprais
        4
    liprais  
       2018-09-29 18:00:37 +08:00
    @hisune 大概意思就这样的,你自己改改就行了,其实不就是把连续量改成离散量呗
    hisune
        5
    hisune  
    OP
       2018-09-29 18:11:16 +08:00
    @liprais
    ```
    select elt(interval(a, 0, 100, 500), 'less100', '0to100', '100to500' 'more500') l, count(uid)
    from
    (select sum(money) as a,uid from orders group by uid) t
    group by l
    ```
    就是实现这种效果,我只能想到用这种方式
    hisune
        6
    hisune  
    OP
       2018-09-29 18:11:26 +08:00
    @GTim 是的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2718 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 05:26 · PVG 13:26 · LAX 22:26 · JFK 01:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.