selectcount(*)as number ,BeginLongitude , BeginLatitude FROM Order_Data where FROM_UNIXTIME(BeginTime)<'2016-11-01 00:05:00'GROUPBY 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的前五位后汇总
selectcount(*)as number ,left(Geo,5)as Geohash FROM Order_Data where FROM_UNIXTIME(BeginTime)<'2016-11-01 24:00:00'GROUPBYleft(Geo,5)
计算demand需要一个变量
SET@sum=(SELECTSUM(number)FROM1101day);SELECT Geohash,number,number/@sumas demand FROM(selectleft(`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')groupbyleft(`order_data`.`Geo`,5)orderby`number`desc)AS T
不用变量写嵌套查询
SELECT Geohash,number,number/(SELECTcount(0)FROM Order_Data where FROM_UNIXTIME(BeginTime)<'2016-11-01 24:00:00')as demand FROM(selectleft(`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')groupbyleft(`order_data`.`Geo`,5)orderby`number`desc)AS T;
取前6位Geohash相等的7位Geohash并分组排序计算
SELECT Geohash,number,CAST(number/(SELECTcount(0)FROM
Order_Data where FROM_UNIXTIME(BeginTime)<'2016-11-01 24:00:00'andleft(`Geo`,6)='wm6n2k')asCHAR(15))as demand FROM(selectleft(`Geo`,7)AS Geohash,count(0)AS number from`order_data`where from_unixtime(`order_data`.`BeginTime`)<'2016-11-01 24:00:00'andleft(`Geo`,6)='wm6n2k'GROUPBY Geohash ORDERBY 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;
'''