要实现MySQL中的group_concat 功能
Clickhouse需要用到三个函数组合使用:
groupArray 行转列
groupUniqArray 等同于mysql中的 group_concat(distinct ..)
arrayStringConcat 等同于group_concat 子语句中的separator '-' 语句
这是一个KPI表,可以很好的理解这三个函数,实现了把每个月的绩效拼接,并记录次数。
emp_no|performance|month |
------|-----------|-------|
10|A |2020-01|
10|A |2020-02|
10|C |2020-03|
10|B |2020-04|
10|A |2020-05|
10|A |2020-06|
20|A |2020-01|
20|B |2020-02|
20|C |2020-03|
20|C |2020-04|
20|A |2020-05|
20|D |2020-06|
30|C |2020-03|
30|C |2020-04|
30|B |2020-05|
30|B |2020-06|
clickhouse组内拼接的实现
SELECT
emp_no,
groupArray(performance) AS kpi_asc,
arrayStringConcat(kpi_asc, '-') AS kpi_list,
arrayReverse(kpi_asc) AS kpi_desc,
groupUniqArray(performance) AS kpis,
arraySort(kpis) AS kpi_uniq,
countEqual(kpi_asc, 'A') AS A_cnt,
countEqual(kpi_asc, 'B') AS B_cnt,
countEqual(kpi_asc, 'C') AS C_cnt,
countEqual(kpi_asc, 'D') AS D_cnt
FROM kpi
GROUP BY emp_no
ORDER BY emp_no ASC
emp_no|kpi_asc |kpi_list |kpi_desc |kpis |kpi_uniq |A_cnt|B_cnt|C_cnt|D_cnt|
------|-------------------------|-----------|-------------------------|-----------------|-----------------|-----|-----|-----|-----|
10|['A','A','C','B','A','A']|A-A-C-B-A-A|['A','A','B','C','A','A']|['B','A','C'] |['A','B','C'] | 4| 1| 1| 0|
20|['A','B','C','C','A','D']|A-B-C-C-A-D|['D','A','C','C','B','A']|['B','D','A','C']|['A','B','C','D']| 2| 1| 2| 1|
30|['C','C','B','B'] |C-C-B-B |['B','B','C','C'] |['B','C'] |['B','C'] | 0| 2| 2| 0|
kpi_list:按照月份依次显示每个月的绩效
kpi_uniq:上半年获得的绩效 等级(绩效去重)
kpi_uniq_desc :去重后的绩效反向排序
原文链接:https://blog.csdn.net/vkingnew/article/details/107730452