首页
注册
登录
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请
登录
V2EX 提问指南
广告
V2EX
›
问与答
求教大佬,这种统计 sql 如何写?
hisune
·
2018-09-29 17:53:53 +08:00
· 1333 次点击
这是一个创建于 2190 天前的主题,其中的信息可能已经有所发展或是发生改变。
金额|账号数量
---|---
2000~4999|
5000~9999|
就是从订单的流水数据中统计每个充值区间段的用户 id 数量有多少,假定数据库表结构是这样的:
money
uid
uid
SQL
求教
假定
6 条回复
•
2018-09-29 18:11:26 +08:00
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(*) 就行了
2
hisune
OP
2018-09-29 17:58:22 +08:00
@
liprais
你这个是订单金额是某个区间的数量吧?其实我要的是用户累计订单金额落在区间的用户数量
3
GTim
2018-09-29 17:59:50 +08:00
每个段的大小不一样?
4
liprais
2018-09-29 18:00:37 +08:00
@
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
```
就是实现这种效果,我只能想到用这种方式
6
hisune
OP
2018-09-29 18:11:26 +08:00
@
GTim
是的
关于
·
帮助文档
·
博客
·
API
·
FAQ
·
实用小工具
·
2410 人在线
最高记录 6679
·
Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 25ms ·
UTC 04:21
·
PVG 12:21
·
LAX 21:21
·
JFK 00:21
Developed with
CodeLauncher
♥ Do have faith in what you're doing.