clickhouse - 如何旋转子组?
问题描述
我们为 Clickhouse 创建了一个平面表,并试图从该表中获取记录以创建一个物化视图。逻辑是,如果 e_id 为 null,则记录为“TypeB”,如果 e_id 不为 null,则记录为“TypeA”。TypeA 和 TypeB 记录都将具有相同的 p_id 和 s_id。我们希望为每个 p_id+s_id 组合创建一条记录。
下面给出的查询适用于过滤器(p_id = 1 和 s_id = 1)但没有过滤器 - 例外是“DB::Exception:标量子查询返回多行”是否可以在 ClickHouse 中执行此操作?是否可以使用这样的查询创建物化视图?
select p_id,s_id,
groupArray(e_id),
groupArray(name),
(select groupArray(name)
from flat_table
where e_id is null and p_id =1 and s_id = 1
group by p_id,s_id) as typeB
from flat_table
where e_id is not null and p_id =1 and s_id = 1
group by p_id,s_id;
/*
This what the table looks like:
Flat_table
p_id s_id e_id name
1 1 1 Jake
1 1 2 Bob
1 1 null Barby
1 1 null Ella
This is expected result:
p_id s_id e_id typeA typeB
1 1 [1,2] [Jake,Bob] [Barby,Ella]
*/
解决方案
Let's try this query:
SELECT p_id, s_id, e_ids, typeA, typeB
FROM (
SELECT
p_id,
s_id,
groupArray((e_id, name)) eid_names,
arrayMap(x -> x.1, arrayFilter(x -> not isNull(x.1), eid_names)) e_ids,
arrayMap(x -> x.2, arrayFilter(x -> not isNull(x.1), eid_names)) typeA,
arrayMap(x -> x.2, arrayFilter(x -> isNull(x.1), eid_names)) typeB
FROM test.test_006
GROUP BY p_id, s_id)
/* Result
┌─p_id─┬─s_id─┬─e_ids─┬─typeA────────────┬─typeB──────────────┐
│ 2 │ 2 │ [1,2] │ ['Jake2','Bob2'] │ ['Barby2','Ella2'] │
│ 1 │ 1 │ [1,2] │ ['Jake','Bob'] │ ['Barby','Ella'] │
└──────┴──────┴───────┴──────────────────┴────────────────────┘
*/
/* Data preparation queries */
CREATE TABLE test.test_006
(
`p_id` Int32,
`s_id` Int32,
`e_id` Nullable(Int32),
`name` String
)
ENGINE = Memory
INSERT INTO test.test_006
VALUES (1, 1, 1, 'Jake'), (1, 1, 2, 'Bob'), (1, 1, null, 'Barby'), (1, 1, null, 'Ella'),
(2, 2, 1, 'Jake2'), (2, 2, 2, 'Bob2'), (2, 2, null, 'Barby2'), (2, 2, null, 'Ella2')
推荐阅读
- python - Python:max(作为字符的数字列表)给出正确和错误的答案
- c# - 为什么我的课程可以在程序类中访问而无需提及访问修饰符?
- google-chrome - 如何在 heroku free dyno 中以非无头模式运行 chrome
- css - 我应该参考哪个单位的宽度以及我应该使用什么单位进行响应式设计?(非自适应设计)
- c++ - OpenGL,C++ 我不断收到错误:'int main(int, char**)' 以前在 C++ 中定义
- reactjs - React 中的延迟加载和列表虚拟化有什么区别?
- python - pandas json规范化获取对象索引中的值
- mongodb - 在猫鼬中,当我想向已经创建的字段添加新内容时,save()、insertOne() 和 updateOne() 有什么区别?
- php - WordPress - 根据首字母列出类别
- r - 将多个因子列组合成 dplyr 中的单个因子