摘要:統計每個分組里的最大最小記錄實現完整見需求得到每個擁有最多人口的城市和擁有最小人口的城市以及對應的人口數效果見實現相比的直觀就要繞很多了方案一需要設置一個較大值默認的還不夠用參考方案二每個分組里面分別按升序降序排序人為分配一個序號均取序號
統計每個分組里的最大最小記錄 mongo實現
{ "_id" : "01001", "city" : "AGAWAM", "pop" : 15338, "state" : "MA" }
完整json見: http://media.mongodb.org/zips...
需求得到每個state擁有最多人口的城市和擁有最小人口的城市以及對應的人口數
db.zipcodes.aggregate( {$group: {_id:{state:"$state",city:"$city"}, popPerCity:{$sum:"$pop"} } }, {$sort: {popPerCity:1} }, {$group: { _id:"$_id.state", biggestCity:{$last:"$_id.city"}, biggestPop: {$last:"$popPerCity"}, smallestCity: {$first:"$_id.city"}, smallestPop: {$first:"$popPerCity"} }} )
效果
{ "_id" : "DE", "biggestCity" : "NEWARK", "biggestPop" : 111674, "smallestCity" : "BETHEL", "smallestPop" : 108 } { "_id" : "MS", "biggestCity" : "JACKSON", "biggestPop" : 204788, "smallestCity" : "CHUNKY", "smallestPop" : 79 } ...
見: https://docs.mongodb.com/manu...
Mysql實現相比mongo的直觀 就要繞很多了
方案一# 需要設置一個較大值 默認的1024還不夠用 SET SESSION group_concat_max_len = 20480; 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...
# 每個state分組里面分別按pop升序、降序排序 人為分配一個序號 均取序號一就得到了該分組的起止記錄 select b.state, b.city smallestCity, b.pop smallestPop, c.city biggestCity, c.pop biggestPop from ( select state,city,pop,@rank:=if(@current_state=state, @rank+1, 1) rank, @current_state:=state from (select state, city, sum(pop) pop from zipcode group by state, city) a, (select @current_state:=NULL, @rank:=NULL) vars order by a.state,a.pop ) b , ( select state,city,pop,@rank:=if(@current_state=state, @rank+1, 1) rank, @current_state:=state from (select state, city, sum(pop) pop from zipcode group by state, city) a, (select @current_state:=NULL, @rank:=NULL) vars order by a.state,a.pop desc ) c where b.state = c.state and b.rank = 1 and c.rank = 1補充
建表語句
CREATE TABLE `zipcode` ( `id` int(11) NOT NULL AUTO_INCREMENT, `zipcode` varchar(10) NOT NULL, `city` varchar(30) NOT NULL, `pop` int(11) NOT NULL, `state` varchar(5) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `zipcode` (`zipcode`), KEY `idx_state_city` (`state`,`city`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
json ==> batch insert sql
jq -c "[._id, .city, .pop, .state]" zips.json | sed "s/[(.*)]$/1/" | awk -F, "{print "insert into zipcode select null," $1"," $2","$3","$4";"}"
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/19020.html
摘要:在第行中,我們會從集合取得結果并顯示它。的邏輯在中,我們要以性別作為,然后以作為。年齡是用來做計算用的,而名字只是用來顯示給人看的。我們要檢查所有和性別相關的年齡,找到年齡最大和最小的用戶。 在這篇文章里面,我們會演示如何在 MongoDB 中使用 MapReduce 操作。我們會用 dummy-json 這個包來生成一些虛假的數據,然后用 Mongojs 如果想要快速看到結果,可以到...
摘要:之所以把計數排序桶排序基數排序放在一起比較,是因為它們的平均時間復雜度都為。動畫計數排序思想找出待排序的數組中最大和最小的元素。桶排序計數排序能派上用場嗎手機號碼有位,范圍太大,顯然不適合用這兩種排序算法。 showImg(https://segmentfault.com/img/bVbuF9e?w=900&h=500); 1. 前言 算法為王。 想學好前端,先練好內功,只有內功深厚者...
閱讀 3350·2021-11-04 16:10
閱讀 3846·2021-09-29 09:43
閱讀 2692·2021-09-24 10:24
閱讀 3338·2021-09-01 10:46
閱讀 2503·2019-08-30 15:54
閱讀 585·2019-08-30 13:19
閱讀 3232·2019-08-29 17:19
閱讀 1049·2019-08-29 16:40