hisune
V2EX  ›  问与答

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

  •  
  •   hisune · Sep 29, 2018 · 2240 views
    This topic created in 2959 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
    F281M6Dh8DXpD1g2
    Sep 29, 2018
    1
    充值金额改成区间不就行了

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