您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“怎么使用SQL實現車流量的計算”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“怎么使用SQL實現車流量的計算”這篇文章吧。
將數據導入hive,通過SparkSql編寫sql,實現不同業務的數據計算實現,主要講述車輛卡口轉換率,卡口轉化率:主要計算不同卡口下車輛之間的流向,求出之間的轉換率。
select car, monitor_id, action_time, ROW_NUMBER () OVER (PARTITION by car ORDER by action_time) as n1 FROM traffic.hive_flow_action
此結果做為表1,方便后面錯位連接使用
通過表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)
獲取到每輛車的一個行車記錄,經過的卡口
獲取卡口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)
獲取每個卡口最初的車輛數,方便后面拿行車軌跡車輛數/總車輛數,得出卡口之間的轉換率
select monitor_id , COUNT(1) sumall from traffic.hive_flow_action group by monitor_id
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實現車流量的計算”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。