根据另外两列将一列转换为多列

问题描述 投票:0回答:2

我在Clickhouse中有以下数据

日期 价值 类型 亚型
2021-01-01 1 A x
2021-01-01 2 A y
2021-01-01 3 B x
2021-01-01 4 B y
2021-01-02 5 A x
2021-01-02 6 A y
2021-01-02 7 B x
2021-01-02 8 B y

我需要将其转化为以下形式

日期 斧头 哎呀 Bx
2021-01-01 1 2 3 4
2021-01-02 5 6 7 8

类型和子类型的组合是已知的。

我尝试了以下查询

SELECT
    date,
    case when type = 'A' and subtype = 'x' then value end Ax,
    case when type = 'A' and subtype = 'y' then value end Ay,
    case when type = 'B' and subtype = 'x' then value end Bx,
    case when type = 'B' and subtype = 'y' then value end By
FROM my_table

但它又回来了

日期 斧头 哎呀 Bx
2021-01-01 1 v
2021-01-02 2 v
2021-01-01 3
2021-01-02 4
2021-01-01 5
2021-01-02 6
2021-01-01 7
2021-01-02 8

我应该怎么做才能创建我需要的没有空值的数据

sql clickhouse
2个回答
0
投票

date
分组并使用
any
filter
子句来选择正确的值:

select
    date,
    any(value) filter (where type = 'A' and subtype = 'x') Ax,
    any(value) filter (where type = 'A' and subtype = 'y') Ay,
    any(value) filter (where type = 'B' and subtype = 'x') Bx,
    any(value) filter (where type = 'B' and subtype = 'y') By
from my_table
group by date

any
将选择与过滤器匹配的第一个值。

输出:

"date","Ax","Ay","Bx","By"
"2021-01-01",1,2,3,4
"2021-01-02",5,6,7,8

完整代码:

create table my_table (
    date Date,
    value Int32,
    type String,
    subtype String
)
engine = MergeTree()
order by ();

insert into my_table (date, value, type, subtype)
values
    ('2021-01-01', 1, 'A', 'x'),
    ('2021-01-01', 2, 'A', 'y'),
    ('2021-01-01', 3, 'B', 'x'),
    ('2021-01-01', 4, 'B', 'y'),
    ('2021-01-02', 5, 'A', 'x'),
    ('2021-01-02', 6, 'A', 'y'),
    ('2021-01-02', 7, 'B', 'x'),
    ('2021-01-02', 8, 'B', 'y');

select
    date,
    any(value) filter (where type = 'A' and subtype = 'x') Ax,
    any(value) filter (where type = 'A' and subtype = 'y') Ay,
    any(value) filter (where type = 'B' and subtype = 'x') Bx,
    any(value) filter (where type = 'B' and subtype = 'y') By
from my_table
group by date
format CSVWithNames;

0
投票

您正在寻找标准的数据透视查询。 在这种情况下,您应该使用

GROUP BY
按日期聚合,然后采用各种
MAX()
表达式的
CASE

SELECT
    date,
    MAX(case when type = 'A' and subtype = 'x' then value end) Ax,
    MAX(case when type = 'A' and subtype = 'y' then value end) Ay,
    MAX(case when type = 'B' and subtype = 'x' then value end) Bx,
    MAX(case when type = 'B' and subtype = 'y' then value end) By
FROM my_table
GROUP BY date
ORDER BY date;
© www.soinside.com 2019 - 2024. All rights reserved.