clickhouse MPPDB数据库 实现复杂功能的SQL示例

环境:

sql 复制代码
clickhouse-client -m -h CH01--port 9001 -d test

伪MAC判断

sql 复制代码
CK通过SQL判断假MAC的示例:  select has(['2','6','A','E','a','e'],SUBSTRING('03-92-26-5B-13-02',2,1) )

将MAC地址替换为字段名即可

1.相似车分析

1.1执行

sql 复制代码
SELECT length(arrayFilter((x, y) -> (x != y), arrayMap(x -> substringUTF8(HPHM, x + 1, 1), range(7)), b)) AS diffLen,HPHM
	FROM
(SELECT PLATE_NO AS HPHM,['滇', '0', '1', 'A', '2', '2', '1'] AS b from CAR_DETECT_RL)
	WHERE diffLen < 2 limit 10;
	
	
SELECT length(arrayFilter((x, y) -> (x != y), arrayMap(x -> substringUTF8(HPHM, x + 1,1), range(7)), b)) AS diffLen,HPHM FROM (SELECT '滇04A221' AS HPHM,['滇', '0', '1', 'A', '2', '2', '1'] AS b from numbers(3)) WHERE diffLen < 2 limit 10;

1.2结果

tex 复制代码
┌─diffLen─┬─HPHM─────┐
│       1 │ 滇04A221 │
└─────────┴──────────┘
┌─diffLen─┬─HPHM─────┐
│       1 │ 滇C1A221 │
└─────────┴──────────┘
┌─diffLen─┬─HPHM─────┐
│       1 │ 滇01AD21 │
└─────────┴──────────┘

2.车辆轨迹搜车

2.1执行

sql 复制代码
--判断车辆实际运行轨迹 是否包含指定的运行轨迹
SELECT
	PLATE_NO,
    length(b) AS len2,
    indexOf(a, b[1]) AS idx,
    length(a) AS len1,
    if(len1 < (len2 + idx), 100, length(arrayFilter(x -> ((a[idx + x]) != (b[x + 1])), range(len2)))) AS diffLen
FROM
(
    select PLATE_NO,groupArray(DEVICE_ID) AS a,arrayDistinct(['4401000000131000301', '494583076246736505','4401000000131000320']) 
    AS b from (
    	SELECT PLATE_NO,DEVICE_ID
    	FROM CAR_DETECT_RL where PASS_TIME between '2019-07-01 00:00:00' and '2019-07-01 23:59:59' order by PASS_TIME
    ) t group by PLATE_NO
) where diffLen > 2;


SELECT PLATE_NO,
	length(b) AS len2,
	indexOf(a, b[1]) AS idx,
	length(a) AS len1,
	if(len1 < (len2 + idx), 100, length(arrayFilter(x -> ((a[idx + x]) != (b[x + 1])), range(len2)))) AS diffLen 
	FROM ( 
		select '浙4C12C2' as PLATE_NO,
		array('4401000000131000301', '494583076246736505','4401000000131000320') AS a,arrayDistinct(['4401000000131000301', '494583076246736505','4401000000131000320']
 	) AS b ) where diffLen > 2;

2.2结果

tex 复制代码
┌─PLATE_NO─┬─len2─┬─idx─┬─len1─┬─diffLen─┐
│ 浙4C12C2 │    3 │  11 │   15 │       1 │
│ 湘2D44DB │    3 │   3 │    9 │       1 │
│ 鲁B1BB1D │    3 │   4 │    7 │       1 │
│ 鲁B1CA10 │    3 │   2 │   13 │       1 │
│ 湘BAB2A2 │    3 │  11 │   16 │       1 │
│ 赣A022B2 │    3 │   4 │   10 │       1 │
│ 鲁D2B410 │    3 │   7 │   11 │       1 │
│ 云DD002C │    3 │  11 │   15 │       1 │

2.3 测试SQL

sql 复制代码
SELECT a,b,if(len1<len2+idx,100,length(arrayFilter(x-> (a[idx+x] != b[x+1]),range(len2)))) as diffLen  
FROM ( 
	SELECT  arrayDistinct(['A', 'B', 'C', 'D','D','D', 'H', 'F', 'G']) AS a,
	arrayDistinct(['B', 'C', 'D', 'E', 'F']) AS b,length(a) AS len1,length(b) AS len2,indexOf(a,b[1]) as idx) 
where diffLen<2;
相关推荐
lucky_syq5 分钟前
Hive SQL和Spark SQL的区别?
hive·sql·spark
m0_7482448315 分钟前
StarRocks 排查单副本表
大数据·数据库·python
C++忠实粉丝25 分钟前
Redis 介绍和安装
数据库·redis·缓存
wmd1316430671240 分钟前
将微信配置信息存到数据库并进行调用
数据库·微信
是阿建吖!1 小时前
【Linux】基础IO(磁盘文件)
linux·服务器·数据库
凡人的AI工具箱1 小时前
每天40分玩转Django:Django国际化
数据库·人工智能·后端·python·django·sqlite
ClouGence1 小时前
Redis 到 Redis 数据迁移同步
数据库·redis·缓存
m0_748236581 小时前
《Web 应用项目开发:从构思到上线的全过程》
服务器·前端·数据库
苏三说技术1 小时前
Redis 性能优化的18招
数据库·redis·性能优化
Tttian6222 小时前
基于Pycharm与数据库的新闻管理系统(2)Redis
数据库·redis·pycharm