首頁 > 軟體

使用SQL實現車流量的計算的範例程式碼

2022-02-28 16:06:47

卡口轉換率

將資料匯入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 車流量內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com