MySQL分组后统计

MySQL分组后统计

select count(*) as number ,BeginLongitude , BeginLatitude FROM Order_Data where FROM_UNIXTIME(BeginTime) <'2016-11-01 00:05:00' 
GROUP BY BeginLongitude, BeginLatitude

在这里插入图片描述

unix时间戳转换为时间查询

select OrderId,FROM_UNIXTIME(BeginTime) BeginTime,FROM_UNIXTIME(EndTime) EndTime,BeginLongitude,BeginLatitude,EndLongitude,EndLatitude
FROM Order_Data
where FROM_UNIXTIME(BeginTime) BETWEEN '2016-11-01 08:00:00' and '2016-11-01 09:00:00'


select OrderId,FROM_UNIXTIME(BeginTime) BeginTime,FROM_UNIXTIME(EndTime) EndTime,BeginLongitude,BeginLatitude,EndLongitude,EndLatitude
FROM Order_Data
where FROM_UNIXTIME(BeginTime) BETWEEN '2016-11-01 22:00:00' and '2016-11-01 23:00:00'

取Geo的前五位后汇总

select count(*) as number ,left(Geo,5) as Geohash FROM Order_Data where FROM_UNIXTIME(BeginTime) <'2016-11-01 24:00:00' 
GROUP BY left(Geo,5)

在这里插入图片描述

计算demand需要一个变量

SET @sum = (SELECT SUM(number)FROM 1101day);SELECT Geohash,number,number/@sum as demand FROM (select left(`order_data`.`Geo`,5) AS `Geohash`,count(0) AS `number` from `order_data` where (from_unixtime(`order_data`.`BeginTime`) < '2016-11-01 24:00:00') group by left(`order_data`.`Geo`,5) order by `number` desc) AS T

在这里插入图片描述

不用变量写嵌套查询

SELECT Geohash,number,number/(SELECT count( 0 )  FROM Order_Data where FROM_UNIXTIME(BeginTime) <'2016-11-01 24:00:00') as demand FROM (select left(`order_data`.`Geo`,5) AS `Geohash`,count(0) AS `number` from `order_data` where (from_unixtime(`order_data`.`BeginTime`) < '2016-11-01 24:00:00') 
group by left(`order_data`.`Geo`,5) order by `number` desc) AS T;

在这里插入图片描述

取前6位Geohash相等的7位Geohash并分组排序计算

SELECT Geohash,number,CAST(number/(SELECT count( 0 ) FROM 
Order_Data where FROM_UNIXTIME(BeginTime) <'2016-11-01 24:00:00' and left(`Geo`,6)='wm6n2k' ) 
as CHAR(15))as demand FROM
(select left(`Geo`,7) AS Geohash,count(0) AS number from `order_data` where from_unixtime(`order_data`.`BeginTime`)< '2016-11-01 24:00:00' 
and left(`Geo`,6)='wm6n2k' GROUP BY Geohash ORDER BY number desc) as T 

在这里插入图片描述

import MySQLdb

# SQL
# 向Student表中从100开始插入1000条随机数据
Student='''
    set @i := 100;
    insert into Student select @i := @i + 1, substr(concat(sha1(rand()*10) + sha1(rand())),1,3+floor(rand()*75)),case floor(rand()*10) mod 2 when 1 then 'M' else 'F' end, 20+floor(rand() * 5) from TMP a,TMP b,TMP c;
'''

发布了26 篇原创文章 · 获赞 1 · 访问量 1743
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览