求教 mysql 查询语句。mysql 数据格式如下,表中 app 重复。需求:取出 一段时间内(四天内吧) 每天每个 appid 调用总量( count 总量)最大的前两个 appid

2015-10-26 10:33:47 +08:00
 luefei

logtime appid count
100018 1226461
1002495 6165611
2015-10-1 1000255 156164611
100018 4964610
1002495 16461661
1000255 46461168

100018      196494561
            1002495     416461616
            1000255     616113

2015-10-2 100018 16461131
1002495 16461613
1000255 1641351

100018      2646116
            1002495     6116161
            1000255     126461611

2015-10-3 100018 548531
1002495 54136116
1000255 496811

100018      41646161
            1002495     1644131
            1000255     61164611

2015-10-4 100018 443134
1002495 15133
1000255 1641434

3559 次点击
所在节点    MySQL
27 条回复
luefei
2015-10-26 10:35:19 +08:00
。。。这个格式。。好蛋疼。。
luefei
2015-10-26 10:35:33 +08:00
logtime appid count
100018 1226461
1002495 6165611
2015-10-1 1000255 156164611
100018 4964610
1002495 16461661
1000255 46461168

100018 196494561
1002495 416461616
1000255 616113
2015-10-2 100018 16461131
1002495 16461613
1000255 1641351

100018 2646116
1002495 6116161
1000255 126461611
2015-10-3 100018 548531
1002495 54136116
1000255 496811

100018 41646161
1002495 1644131
1000255 61164611
2015-10-4 100018 443134
1002495 15133
1000255 1641434
luefei
2015-10-26 10:45:07 +08:00
logtime appid count
100018 1226461
1002495 6165611
2015-10-1 1000255 156164611
100018 4964610
1002495 16461661
1000255 46461168

100018 196494561
1002495 416461616
1000255 616113
2015-10-2 100018 16461131
1002495 16461613
1000255 1641351

100018 2646116
1002495 6116161
1000255 126461611
2015-10-3 100018 548531
1002495 54136116
1000255 496811

100018 41646161
1002495 1644131
1000255 61164611
2015-10-4 100018 443134
1002495 15133
1000255 1641434 这样的格式 讲究下吧,各位大牛求指导。
herojaxy
2015-10-26 10:46:59 +08:00
这排版...没看懂啥意思
luefei
2015-10-26 11:08:44 +08:00
@herojaxy 额 数据格式三列 日期 appid 调用量, 结果 发出去后 日期那一列就合进去了。第一次发问。不知道怎么发图片。尝试了两次 这个格式都没调整对。
bugsnail
2015-10-26 12:49:57 +08:00
表名,字段名,必须
数据一行就可以了,只是为了形式
关开格式的问题,不单单只能用空格的,你可以把空格替换成--再发上来都可以.....

最后,自行 google V2EX 发图 ....
HanSonJ
2015-10-26 13:01:02 +08:00
select count(appid) from table where time > 4days group by appid limit 2 order by count(appid) desc
这样取出最大两个的总数,大概就这意思,不知道符不符合
luefei
2015-10-26 14:54:52 +08:00
@bugsnail 谢谢。我重新整理下
luefei
2015-10-26 14:56:23 +08:00
@HanSonJ 这样是没有调用总量的。调用总量 还要求和。
luefei
2015-10-26 14:57:59 +08:00
@bugsnail
logtime appid count
------------100018 1226461
------------1002495 6165611
2015-10-1 1000255 156164611
100018 4964610
1002495 16461661
1000255 46461168

100018 196494561
1002495 416461616
1000255 616113
2015-10-2 100018 16461131
1002495 16461613
1000255 1641351

100018 2646116
1002495 6116161
1000255 126461611
2015-10-3 100018 548531
1002495 54136116
1000255 496811

100018 41646161
1002495 1644131
1000255 61164611
2015-10-4 100018 443134
1002495 15133
1000255 1641434
luefei
2015-10-26 15:02:40 +08:00
logtime appid count
------------ ------100018 1226461
-------------------1002495 6165611
2015-10-1 1000255 156164611
-------------------100018 4964610
-------------------1002495 16461661
-------------------1000255 46461168

-------------------100018 196494561
-------------------1002495 416461616
-------------------1000255 616113
2015-10-2 100018 16461131
-------------------1002495 16461613
-------------------1000255 1641351

-------------------100018 2646116
-------------------1002495 6116161
-------------------1000255 126461611
2015-10-3 100018 548531
-------------------1002495 54136116
-------------------1000255 496811

-------------------100018 41646161
-------------------1002495 1644131
-------------------1000255 61164611
2015-10-4 100018 443134
-------------------1002495 15133
-------------------1000255 1641434
popok
2015-10-26 15:05:34 +08:00
@luefei 你还是直接图片随便上传一个地方,贴图片地址吧
iyaozhen
2015-10-26 15:07:55 +08:00
取 4 天?每天的调用量最大的 2 个?
popok
2015-10-26 15:17:05 +08:00
select sum(count) from 表名 group by appid where logtime>2015-10-20 and logtime<2015-10-24 limit 0,2

我猜是这样,没测试过
popok
2015-10-26 15:20:56 +08:00
select sum(count) from 表名 where logtime>2015-10-20 and logtime<2015-10-24 group by appid order by sum(count) desc limit 0,2
luefei
2015-10-26 15:24:54 +08:00
@popok 恩恩。我看下重发个问题吧。这个第一次提问,搞砸了。。
popok
2015-10-26 15:26:42 +08:00
@luefei 你的问题描述其实很简单,直接一个表头就说明白了,不需要截图了
popok
2015-10-26 15:41:04 +08:00
select appid,sum(count) from 表名 where logtime>2015-10-20 and logtime<2015-10-24 group by appid order by 2 desc limit 0,2

这个应该就是结果了
bugsnail
2015-10-26 15:41:06 +08:00
结构不知道是不是这样...
![]( http://7xiwkv.com1.z0.glb.clouddn.com/v2test.png)

sql:

```
select login_date,appid,count
from t_name
where UNIX_TIMESTAMP('2015-10-22') BETWEEN UNIX_TIMESTAMP(CURDATE())-3600*24*4 and UNIX_TIMESTAMP(CURDATE())
group by login_date,appid
order by login_date,count desc
```
那个 22 号改为当前日期可以查看过去 4 天的,那个分组前两条不会了,请大神....
luefei
2015-10-26 15:42:46 +08:00
@popok 恩恩。算是比较复杂了。我写出来的语句 太复杂 效率太低 用不了。。所以还是请教帮忙,技术水平有限。 select a.* from (select logTime, appid, sum(count) as dailycount from table where datediff(current_date(), logTime) < 4 group by logTime, appid order by logTime, dailycount desc) as a where 2 > (select count(*) from (select logTime, appid, sum(count) as b from table where datediff(current_date(), logTime) group by logTime, appid order by logTime, dailycount desc) as b where a.logTime = b.logTime and b.dailycount > a.dailycount);

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/231087

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX