本文主要分为以下几部分:
1、创建每个人的公交车起点和终点表
2、创建每个人的地铁起点和终点表
3、创建非工作日白天地铁od平均值视图
4、创建非工作日白天公交车od视图
5、创建非工作日晚上地铁od平均值
6、创建非公工作日晚上公交车到商圈od量平均值视图
7、创建非工作日白天出租车到商圈的od视图
8、创建非工作日晚上出租车到商圈的od
9、创建工作日白天出租车到商圈的od视图
10、创建工作日白天地铁od平均值
11、创建工作日白天公交车到商圈od量视图
12、创建工作日晚上出租车到商圈的od视图
13、创建工作日晚上地铁od平均值
14、创建工作日晚上公交车od平均值
以下为具体的sql语句,实现各种od视图
create table public.mall_bus_od
(
"date_hour" text,
"start_grid" text,
"end_grid" text,
"od_number" bigint,
"shangquanid" bigint,
"shangquanname" text
);
---给字段添加注释
comment on column mall_bus_od.date_hour is '日期和小时';
comment on column mall_bus_od.start_grid is '网格起点';
comment on column mall_bus_od.end_grid is '网格终点';
comment on column mall_bus_od.od_number is '出行od量';
comment on column mall_bus_od.shangquanid is '所在商圈id';
comment on column mall_bus_od.shangquanname is '所在商圈名称';
CREATE TABLE p_metro_od_station_sz (
"station_name_origin" TEXT,
"station_name_destination" TEXT,
"od_count" INTEGER,
"time" TIMESTAMP WITHOUT TIME ZONE
)
create view metro_unwork_daytime_od_station_geom as
SELECT
work_data.station_name_origin,
work_data.station_name_destination,
ROUND(avg(work_data.od_count), 0) as work_day_avg,
work_data.longitude_origin,
work_data.latitude_origin,
work_data.longitude_destination,
work_data.latitude_destination,
work_data.geom
FROM
(
SELECT
*
FROM
r_metro_od_station_geom metro_od
WHERE
metro_od."time" BETWEEN '2017-04-03 06:00:00'
AND '2017-04-03 18:00:00' UNION
SELECT
*
FROM
r_metro_od_station_geom metro_od
WHERE
metro_od."time" BETWEEN '2017-04-04 06:00:00'
AND '2017-04-04 18:00:00' UNION
SELECT
*
FROM
r_metro_od_station_geom metro_od
WHERE
metro_od."time" BETWEEN '2017-04-08 06:00:00'
AND '2017-04-08 18:00:00' UNION
SELECT
*
FROM
r_metro_od_station_geom metro_od
WHERE
metro_od."time" BETWEEN '2017-04-09 06:00:00'
AND '2017-04-09 18:00:00'
) as work_data
GROUP BY station_name_origin,station_name_destination,longitude_origin,latitude_origin,longitude_destination,latitude_destination,geom
create view bus_unwork_daytime_od_geom as
SELECT
work_data.start_grid,
work_data.end_grid,
ROUND(avg(work_data.od_count), 0) as work_day_avg,
work_data.longitude_origin,
work_data.latitude_origin,
work_data.longitude_mall,
work_data.latitude_mall,
work_data.mall_id,
work_data.mall_name,
work_data.geom
FROM
(
SELECT
*
FROM
mall_bus_od_geom
WHERE
mall_bus_od_geom."time" BETWEEN '2017-04-03 06:00:00'
AND '2017-04-03 18:00:00' UNION
SELECT
*
FROM
mall_bus_od_geom
WHERE
mall_bus_od_geom."time" BETWEEN '2017-04-04 06:00:00'
AND '2017-04-04 18:00:00' UNION
SELECT
*
FROM
mall_bus_od_geom
WHERE
mall_bus_od_geom."time" BETWEEN '2017-04-08 06:00:00'
AND '2017-04-08 18:00:00' UNION
SELECT
*
FROM
mall_bus_od_geom
WHERE
mall_bus_od_geom."time" BETWEEN '2017-04-09 06:00:00'
AND '2017-04-09 18:00:00'
) as work_data
GROUP BY start_grid,end_grid,longitude_origin,latitude_origin,longitude_mall,latitude_mall,mall_id,mall_name,geom
create view metro_unwork_night_od_station_geom as
SELECT
work_data.station_name_origin,
work_data.station_name_destination,
ROUND(avg(work_data.od_count), 0) as work_night_avg,
work_data.longitude_origin,
work_data.latitude_origin,
work_data.longitude_destination,
work_data.latitude_destination,
work_data.geom
FROM
(
SELECT
*
FROM
r_metro_od_station_geom metro_od
WHERE
metro_od."time" BETWEEN '2017-04-03 18:00:00'
AND '2017-04-03 24:00:00' UNION
SELECT
*
FROM
r_metro_od_station_geom metro_od
WHERE
metro_od."time" BETWEEN '2017-04-04 18:00:00'
AND '2017-04-04 24:00:00' UNION
SELECT
*
FROM
r_metro_od_station_geom metro_od
WHERE
metro_od."time" BETWEEN '2017-04-08 18:00:00'
AND '2017-04-08 24:00:00' UNION
SELECT
*
FROM
r_metro_od_station_geom metro_od
WHERE
metro_od."time" BETWEEN '2017-04-09 18:00:00'
AND '2017-04-09 24:00:00'
) as work_data
GROUP BY station_name_origin,station_name_destination,longitude_origin,latitude_origin,longitude_destination,latitude_destination,geom
create view bus_unwork_night_od_geom as
SELECT
work_data.start_grid,
work_data.end_grid,
ROUND(avg(work_data.od_count), 0) as unwork_night_avg,
work_data.longitude_origin,
work_data.latitude_origin,
work_data.longitude_mall,
work_data.latitude_mall,
work_data.mall_id,
work_data.mall_name,
work_data.geom
FROM
(
SELECT
*
FROM
mall_bus_od_geom
WHERE
mall_bus_od_geom."time" BETWEEN '2017-04-03 18:00:00'
AND '2017-04-03 24:00:00' UNION
SELECT
*
FROM
mall_bus_od_geom
WHERE
mall_bus_od_geom."time" BETWEEN '2017-04-04 18:00:00'
AND '2017-04-04 24:00:00' UNION
SELECT
*
FROM
mall_bus_od_geom
WHERE
mall_bus_od_geom."time" BETWEEN '2017-04-08 18:00:00'
AND '2017-04-08 24:00:00' UNION
SELECT
*
FROM
mall_bus_od_geom
WHERE
mall_bus_od_geom."time" BETWEEN '2017-04-09 18:00:00'
AND '2017-04-09 24:00:00'
) as work_data
GROUP BY start_grid,end_grid,longitude_origin,latitude_origin,longitude_mall,latitude_mall,mall_id,mall_name,geom
create view taxi_unwork_daytime_od_geom as
SELECT
work_data.grid_id_origin,
work_data.grid_id_destination,
ROUND(avg(work_data.od_count), 0) as unwork_day_avg,
work_data.longitude_origin,
work_data.latitude_origin,
work_data.longitude_destination,
work_data.latitude_destination,
work_data.mall_id,
work_data.mall_name,
work_data.geom
FROM
(
SELECT
*
FROM
mall_taxi_od
WHERE
mall_taxi_od."time" BETWEEN '2017-04-03 06:00:00'
AND '2017-04-03 18:00:00' UNION
SELECT
*
FROM
mall_taxi_od
WHERE
mall_taxi_od."time" BETWEEN '2017-04-04 06:00:00'
AND '2017-04-04 18:00:00' UNION
SELECT
*
FROM
mall_taxi_od
WHERE
mall_taxi_od."time" BETWEEN '2017-04-08 06:00:00'
AND '2017-04-08 18:00:00' UNION
SELECT
*
FROM
mall_taxi_od
WHERE
mall_taxi_od."time" BETWEEN '2017-04-09 06:00:00'
AND '2017-04-09 18:00:00'
) as work_data
GROUP BY grid_id_origin,grid_id_destination,longitude_origin,latitude_origin,longitude_destination,latitude_destination,mall_id,mall_name,geom
create view taxi_unwork_night_od_geom as
SELECT
work_data.grid_id_origin,
work_data.grid_id_destination,
ROUND(avg(work_data.od_count), 0) as unwork_night_avg,
work_data.longitude_origin,
work_data.latitude_origin,
work_data.longitude_destination,
work_data.latitude_destination,
work_data.mall_id,
work_data.mall_name,
work_data.geom
FROM
(
SELECT
*
FROM
mall_taxi_od
WHERE
mall_taxi_od."time" BETWEEN '2017-04-03 18:00:00'
AND '2017-04-03 24:00:00' UNION
SELECT
*
FROM
mall_taxi_od
WHERE
mall_taxi_od."time" BETWEEN '2017-04-04 18:00:00'
AND '2017-04-04 24:00:00' UNION
SELECT
*
FROM
mall_taxi_od
WHERE
mall_taxi_od."time" BETWEEN '2017-04-08 18:00:00'
AND '2017-04-08 24:00:00' UNION
SELECT
*
FROM
mall_taxi_od
WHERE
mall_taxi_od."time" BETWEEN '2017-04-09 18:00:00'
AND '2017-04-09 24:00:00'
) as work_data
GROUP BY grid_id_origin,grid_id_destination,longitude_origin,latitude_origin,longitude_destination,latitude_destination,mall_id,mall_name,geom
create view taxi_work_daytime_od_geom as
SELECT
work_data.grid_id_origin,
work_data.grid_id_destination,
ROUND(avg(work_data.od_count), 0) as work_daytime_avg,
work_data.longitude_origin,
work_data.latitude_origin,
work_data.longitude_destination,
work_data.latitude_destination,
work_data.mall_id,
work_data.mall_name,
work_data.geom
FROM
(
SELECT
*
FROM
mall_taxi_od
WHERE
mall_taxi_od."time" BETWEEN '2017-04-05 06:00:00'
AND '2017-04-05 18:00:00' UNION
SELECT
*
FROM
mall_taxi_od
WHERE
mall_taxi_od."time" BETWEEN '2017-04-06 06:00:00'
AND '2017-04-06 18:00:00' UNION
SELECT
*
FROM
mall_taxi_od
WHERE
mall_taxi_od."time" BETWEEN '2017-04-07 06:00:00'
AND '2017-04-07 18:00:00'
) as work_data
GROUP BY grid_id_origin,grid_id_destination,longitude_origin,latitude_origin,longitude_destination,latitude_destination,mall_id,mall_name,geom
create view metro_work_daytime_od_station_geom as
SELECT
work_data.station_name_origin,
work_data.station_name_destination,
ROUND(avg(work_data.od_count), 0) as work_daytime_avg,
work_data.longitude_origin,
work_data.latitude_origin,
work_data.longitude_destination,
work_data.latitude_destination,
work_data.geom
FROM
(
SELECT
*
FROM
r_metro_od_station_geom metro_od
WHERE
metro_od."time" BETWEEN '2017-04-05 06:00:00'
AND '2017-04-05 18:00:00' UNION
SELECT
*
FROM
r_metro_od_station_geom metro_od
WHERE
metro_od."time" BETWEEN '2017-04-06 06:00:00'
AND '2017-04-06 18:00:00' UNION
SELECT
*
FROM
r_metro_od_station_geom metro_od
WHERE
metro_od."time" BETWEEN '2017-04-07 06:00:00'
AND '2017-04-07 18:00:00'
) as work_data
GROUP BY station_name_origin,station_name_destination,longitude_origin,latitude_origin,longitude_destination,latitude_destination,geom
create view bus_work_daytime_od_geom as
SELECT
work_data.start_grid,
work_data.end_grid,
ROUND(avg(work_data.od_count), 0) as work_daytime_avg,
work_data.longitude_origin,
work_data.latitude_origin,
work_data.longitude_mall,
work_data.latitude_mall,
work_data.mall_id,
work_data.mall_name,
work_data.geom
FROM
(
SELECT
*
FROM
mall_bus_od_geom
WHERE
mall_bus_od_geom."time" BETWEEN '2017-04-05 06:00:00'
AND '2017-04-05 18:00:00' UNION
SELECT
*
FROM
mall_bus_od_geom
WHERE
mall_bus_od_geom."time" BETWEEN '2017-04-06 06:00:00'
AND '2017-04-06 18:00:00' UNION
SELECT
*
FROM
mall_bus_od_geom
WHERE
mall_bus_od_geom."time" BETWEEN '2017-04-07 06:00:00'
AND '2017-04-07 18:00:00'
) as work_data
GROUP BY start_grid,end_grid,longitude_origin,latitude_origin,longitude_mall,latitude_mall,mall_id,mall_name,geom
create view taxi_work_night_od_geom as
SELECT
work_data.grid_id_origin,
work_data.grid_id_destination,
ROUND(avg(work_data.od_count), 0) as work_night_avg,
work_data.longitude_origin,
work_data.latitude_origin,
work_data.longitude_destination,
work_data.latitude_destination,
work_data.mall_id,
work_data.mall_name,
work_data.geom
FROM
(
SELECT
*
FROM
mall_taxi_od
WHERE
mall_taxi_od."time" BETWEEN '2017-04-05 18:00:00'
AND '2017-04-05 24:00:00' UNION
SELECT
*
FROM
mall_taxi_od
WHERE
mall_taxi_od."time" BETWEEN '2017-04-06 18:00:00'
AND '2017-04-06 24:00:00' UNION
SELECT
*
FROM
mall_taxi_od
WHERE
mall_taxi_od."time" BETWEEN '2017-04-07 18:00:00'
AND '2017-04-07 24:00:00'
) as work_data
GROUP BY grid_id_origin,grid_id_destination,longitude_origin,latitude_origin,longitude_destination,latitude_destination,mall_id,mall_name,geom
create view metro_work_night_od_station_geom as
SELECT
work_data.station_name_origin,
work_data.station_name_destination,
ROUND(avg(work_data.od_count), 0) as work_night_avg,
work_data.longitude_origin,
work_data.latitude_origin,
work_data.longitude_destination,
work_data.latitude_destination,
work_data.geom
FROM
(
SELECT
*
FROM
r_metro_od_station_geom metro_od
WHERE
metro_od."time" BETWEEN '2017-04-05 18:00:00'
AND '2017-04-05 24:00:00' UNION
SELECT
*
FROM
r_metro_od_station_geom metro_od
WHERE
metro_od."time" BETWEEN '2017-04-06 18:00:00'
AND '2017-04-06 24:00:00' UNION
SELECT
*
FROM
r_metro_od_station_geom metro_od
WHERE
metro_od."time" BETWEEN '2017-04-07 18:00:00'
AND '2017-04-07 24:00:00'
) as work_data
GROUP BY station_name_origin,station_name_destination,longitude_origin,latitude_origin,longitude_destination,latitude_destination,geom
create view bus_work_night_od_geom as
SELECT
work_data.start_grid,
work_data.end_grid,
ROUND(avg(work_data.od_count), 0) as work_night_avg,
work_data.longitude_origin,
work_data.latitude_origin,
work_data.longitude_mall,
work_data.latitude_mall,
work_data.mall_id,
work_data.mall_name,
work_data.geom
FROM
(
SELECT
*
FROM
mall_bus_od_geom
WHERE
mall_bus_od_geom."time" BETWEEN '2017-04-05 18:00:00'
AND '2017-04-05 24:00:00' UNION
SELECT
*
FROM
mall_bus_od_geom
WHERE
mall_bus_od_geom."time" BETWEEN '2017-04-06 18:00:00'
AND '2017-04-06 24:00:00' UNION
SELECT
*
FROM
mall_bus_od_geom
WHERE
mall_bus_od_geom."time" BETWEEN '2017-04-07 18:00:00'
AND '2017-04-07 24:00:00'
) as work_data
GROUP BY start_grid,end_grid,longitude_origin,latitude_origin,longitude_mall,latitude_mall,mall_id,mall_name,geom
如有疑问或需要示例数据表请私聊我。。。