资讯专栏INFORMATION COLUMN

得到每个分组里的最大最小记录 Mongo VS Mysql

gekylin / 1804人阅读

摘要:统计每个分组里的最大最小记录实现完整见需求得到每个拥有最多人口的城市和拥有最小人口的城市以及对应的人口数效果见实现相比的直观就要绕很多了方案一需要设置一个较大值默认的还不够用参考方案二每个分组里面分别按升序降序排序人为分配一个序号均取序号

统计每个分组里的最大最小记录 mongo实现

</>复制代码

  1. {
  2. "_id" : "01001",
  3. "city" : "AGAWAM",
  4. "pop" : 15338,
  5. "state" : "MA"
  6. }

完整json见: http://media.mongodb.org/zips...

需求

</>复制代码

  1. 得到每个state拥有最多人口的城市和拥有最小人口的城市以及对应的人口数

</>复制代码

  1. db.zipcodes.aggregate(
  2. {$group: {_id:{state:"$state",city:"$city"}, popPerCity:{$sum:"$pop"} } },
  3. {$sort: {popPerCity:1} },
  4. {$group: {
  5. _id:"$_id.state",
  6. biggestCity:{$last:"$_id.city"},
  7. biggestPop: {$last:"$popPerCity"},
  8. smallestCity: {$first:"$_id.city"},
  9. smallestPop: {$first:"$popPerCity"}
  10. }}
  11. )

效果

</>复制代码

  1. { "_id" : "DE", "biggestCity" : "NEWARK", "biggestPop" : 111674, "smallestCity" : "BETHEL", "smallestPop" : 108 }
  2. { "_id" : "MS", "biggestCity" : "JACKSON", "biggestPop" : 204788, "smallestCity" : "CHUNKY", "smallestPop" : 79 }
  3. ...

见: https://docs.mongodb.com/manu...

Mysql实现

相比mongo的直观 就要绕很多了

方案一

</>复制代码

  1. # 需要设置一个较大值 默认的1024还不够用
  2. SET SESSION group_concat_max_len = 20480;
  3. select state, substring_index(group_concat(city order by pop ),",",1) smallestCity, min(pop),substring_index(group_concat(city order by pop ),",",-1) biggestCity, max(pop) from (select state, city, sum(pop) pop from zipcode group by state, city) a group by state ;

参考

https://dev.mysql.com/doc/ref...
https://dev.mysql.com/doc/ref...

方案二

</>复制代码

  1. # 每个state分组里面分别按pop升序、降序排序 人为分配一个序号 均取序号一就得到了该分组的起止记录
  2. select b.state, b.city smallestCity, b.pop smallestPop, c.city biggestCity, c.pop biggestPop from
  3. ( select state,city,pop,@rank:=if(@current_state=state, @rank+1, 1) rank, @current_state:=state from
  4. (select state, city, sum(pop) pop from zipcode group by state, city) a,
  5. (select @current_state:=NULL, @rank:=NULL) vars
  6. order by a.state,a.pop
  7. ) b ,
  8. ( select state,city,pop,@rank:=if(@current_state=state, @rank+1, 1) rank, @current_state:=state from
  9. (select state, city, sum(pop) pop from zipcode group by state, city) a,
  10. (select @current_state:=NULL, @rank:=NULL) vars
  11. order by a.state,a.pop desc
  12. ) c
  13. where b.state = c.state and b.rank = 1 and c.rank = 1
补充

建表语句

</>复制代码

  1. CREATE TABLE `zipcode` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `zipcode` varchar(10) NOT NULL,
  4. `city` varchar(30) NOT NULL,
  5. `pop` int(11) NOT NULL,
  6. `state` varchar(5) NOT NULL,
  7. PRIMARY KEY (`id`),
  8. UNIQUE KEY `zipcode` (`zipcode`),
  9. KEY `idx_state_city` (`state`,`city`)
  10. ) ENGINE=InnoDB DEFAULT CHARSET=utf8

json ==> batch insert sql

</>复制代码

  1. jq -c "[._id, .city, .pop, .state]" zips.json | sed "s/[(.*)]$/1/" | awk -F, "{print "insert into zipcode select null," $1"," $2","$3","$4";"}"

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/19020.html

相关文章

  • 「译」 MapReduce in MongoDB

    摘要:在第行中,我们会从集合取得结果并显示它。的逻辑在中,我们要以性别作为,然后以作为。年龄是用来做计算用的,而名字只是用来显示给人看的。我们要检查所有和性别相关的年龄,找到年龄最大和最小的用户。 在这篇文章里面,我们会演示如何在 MongoDB 中使用 MapReduce 操作。我们会用 dummy-json 这个包来生成一些虚假的数据,然后用 Mongojs 如果想要快速看到结果,可以到...

    ConardLi 评论0 收藏0
  • Mongo语法总结

    摘要:先进行过滤,再分组实例解释进行过滤,这里利用两个字段进行过滤。聚合操作可以对分组的数据执行如下的表达式计算计算总和。根据分组,获取集合中所有文档对应值得最大值。将指定的表达式的值添加到一个数组中。 先进行过滤,再分组 1、实例: db.getCollection(UpMsgItem).aggregate( [ {$match : { createTime : {$gt : ...

    shmily 评论0 收藏0
  • JavaScript 数据结构与算法之美 - 桶排序、计数排序、基数排序

    摘要:之所以把计数排序桶排序基数排序放在一起比较,是因为它们的平均时间复杂度都为。动画计数排序思想找出待排序的数组中最大和最小的元素。桶排序计数排序能派上用场吗手机号码有位,范围太大,显然不适合用这两种排序算法。 showImg(https://segmentfault.com/img/bVbuF9e?w=900&h=500); 1. 前言 算法为王。 想学好前端,先练好内功,只有内功深厚者...

    Awbeci 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<