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;
'''

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值