欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > clickhouse MPPDB数据库 实现复杂功能的SQL示例

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

2025/4/6 3:56:25 来源:https://blog.csdn.net/penriver/article/details/141497752  浏览:    关键词:clickhouse MPPDB数据库 实现复杂功能的SQL示例

环境:

clickhouse-client -m -h CH01--port 9001 -d test

伪MAC判断

CK通过SQL判断假MAC的示例:  select has(['2','6','A','E','a','e'],SUBSTRING('03-92-26-5B-13-02',2,1) )将MAC地址替换为字段名即可

1.相似车分析

1.1执行

SELECT length(arrayFilter((x, y) -> (x != y), arrayMap(x -> substringUTF8(HPHM, x + 1, 1), range(7)), b)) AS diffLen,HPHMFROM
(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结果

┌─diffLen─┬─HPHM─────┐
│       1 │ 滇04A221 │
└─────────┴──────────┘
┌─diffLen─┬─HPHM─────┐
│       1 │ 滇C1A221 │
└─────────┴──────────┘
┌─diffLen─┬─HPHM─────┐
│       1 │ 滇01AD21 │
└─────────┴──────────┘

2.车辆轨迹搜车

2.1执行

--判断车辆实际运行轨迹 是否包含指定的运行轨迹
SELECTPLATE_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_IDFROM 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结果

┌─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

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;

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词