使用SQL實現車流量的計算的示例代碼

卡口轉換率

將數據導入hive,通過SparkSql編寫sql,實現不同業務的數據計算實現,主要講述車輛卡口轉換率,卡口轉化率:主要計算不同卡口下車輛之間的流向,求出之間的轉換率。

在這裡插入圖片描述

1、查出每個地區下每個路段下的車流量

select
	car,
	monitor_id,
	action_time,
	ROW_NUMBER () OVER (PARTITION by car
ORDER by
	action_time) as n1
FROM
	traffic.hive_flow_action

此結果做為表1,方便後面錯位連接使用

在這裡插入圖片描述

2、通過錯位連接獲取每輛車的行車記錄

通過表1的結果,與自身進行錯位鏈接,並以車牌為分區,拼接經過卡口的過程

(select
	t1.car,
	t1.monitor_id,
	concat(t1.monitor_id,
	"->",
	t2.monitor_id) as way
from
	(
	select
		car,
		monitor_id,
		action_time,
		ROW_NUMBER () OVER (PARTITION by car
	ORDER by
		action_time) as n1
	FROM
		traffic.hive_flow_action) t1
left join (
	select
		car,
		monitor_id,
		action_time,
		ROW_NUMBER () OVER (PARTITION by car
	ORDER by
		action_time) as n1
	FROM
		traffic.hive_flow_action) t2 on
	t1.car = t2.car
	and t1.n1 = t2.n1-1
where
	t2.action_time is not null)

在這裡插入圖片描述

獲取到每輛車的一個行車記錄,經過的卡口

3、獲取行車過程中的車輛數

獲取卡口1~卡口2,…等的車輛數有哪些,即拿上面的行車記錄字段進行分區在進行統計

(select
	s1.way,
	COUNT(1) sumCar
from
	--行車過程
(select
		t1.car,
		t1.monitor_id,
		concat(t1.monitor_id,
		"->",
		t2.monitor_id) as way
	from
		(
		select
			car,
			monitor_id,
			action_time,
			ROW_NUMBER () OVER (PARTITION by car
		ORDER by
			action_time) as n1
		FROM
			traffic.hive_flow_action) t1
	left join (
		select
			car,
			monitor_id,
			action_time,
			ROW_NUMBER () OVER (PARTITION by car
		ORDER by
			action_time) as n1
		FROM
			traffic.hive_flow_action) t2 on
		t1.car = t2.car
		and t1.n1 = t2.n1-1
	where
		t2.action_time is not null)s1
group by way)

在這裡插入圖片描述

4、獲取每個卡口的總車輛數

獲取每個卡口最初的車輛數,方便後面拿行車軌跡車輛數/總車輛數,得出卡口之間的轉換率

select
	monitor_id ,
	COUNT(1) sumall
from
	traffic.hive_flow_action
group by
	monitor_id

在這裡插入圖片描述

5、求出卡口之間的轉換率

select
	s2.way,
	s2.sumCar / s3.sumall zhl
from
	(
	select
		s1.way,
		COUNT(1) sumCar
	from
		--行車過程
(
		select
			t1.car,
			t1.monitor_id,
			concat(t1.monitor_id,
			"->",
			t2.monitor_id) as way
		from
			(
			select
				car,
				monitor_id,
				action_time,
				ROW_NUMBER () OVER (PARTITION by car
			ORDER by
				action_time) as n1
			FROM
				traffic.hive_flow_action) t1
		left join (
			select
				car,
				monitor_id,
				action_time,
				ROW_NUMBER () OVER (PARTITION by car
			ORDER by
				action_time) as n1
			FROM
				traffic.hive_flow_action) t2 on
			t1.car = t2.car
			and t1.n1 = t2.n1-1
		where
			t2.action_time is not null)s1
	group by
		way)s2
left join
	--每個卡口總車數
(
	select
		monitor_id ,
		COUNT(1) sumall
	from
		traffic.hive_flow_action
	group by
		monitor_id) s3 on
	split(s2.way,
	"->")[0]= s3.monitor_id

在這裡插入圖片描述

 到此這篇關於使用SQL實現車流量的計算的示例代碼的文章就介紹到這瞭,更多相關SQL 車流量內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: