hisune
V2EX  ›  问与答

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

  •  
  •   hisune · Sep 29, 2018 · 1920 views
    This topic created in 2809 days ago, the information mentioned may be changed or developed.
    金额|账号数量
    ---|---
    2000~4999|
    5000~9999|

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

    case when money > 2000 and money <= 4999 then 'a'
    when your-condition-here then 'b'
    else
    end
    这样
    然后 group by count(*) 就行了
    hisune
        2
    hisune  
    OP
       Sep 29, 2018
    @liprais 你这个是订单金额是某个区间的数量吧?其实我要的是用户累计订单金额落在区间的用户数量
    yemoluo
        3
    yemoluo  
       Sep 29, 2018
    每个段的大小不一样?
    F281M6Dh8DXpD1g2
        4
    F281M6Dh8DXpD1g2  
       Sep 29, 2018
    @hisune 大概意思就这样的,你自己改改就行了,其实不就是把连续量改成离散量呗
    hisune
        5
    hisune  
    OP
       Sep 29, 2018
    @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
       Sep 29, 2018
    @GTim 是的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5508 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 89ms · UTC 09:01 · PVG 17:01 · LAX 02:01 · JFK 05:01
    ♥ Do have faith in what you're doing.