獲得每一個分組裏的最大最小記錄 Mongo VS Mysql

統計每一個分組裏的最大最小記錄

mongo實現

{
     "_id" : "01001",
     "city" : "AGAWAM",
     "pop" : 15338,
     "state" : "MA"
}

完整json見: http://media.mongodb.org/zips...html

需求

獲得每一個state擁有最多人口的城市和擁有最小人口的城市以及對應的人口數mysql

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"}
    }}
)

效果sql

{ "_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...mongodb

Mysql實現

相比mongo的直觀 就要繞不少了json

方案一

# 須要設置一個較大值 默認的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 ;

參考code

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

方案二

# 每一個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 sqlip

jq -c '[._id, .city, .pop, .state]' zips.json | sed 's/\[\(.*\)\]$/\1/' | awk -F, '{print "insert into zipcode select null," $1"," $2","$3","$4";"}'
相關文章
相關標籤/搜索