【问题标题】:Pivot In clickhouse在 clickhouse 中枢轴
【发布时间】:2020-09-30 00:30:14
【问题描述】:

我想在 clickhouse 中做一个支点 我有

形式的数据
rule_name | result
'string_1', 'result_1'
'string_2', 'result_2'
'string_3', 'result_3'
'string_4', 'result_4'

我想把它转成这样 string_1, string_2 ... 是列 结果应该有 4 列和 1 行(result_1, result_2, result_3, result_4)

string_1 | string_2 | string_3 | string_4
result_1 | result_2 | result_3 | result_4

┌─string_1────┬─string_2─────┬─string_3─────┬─string_4─────┐
│ result_1      result_2        result_3      result_4
└─────────────┴──────────────┴──────────────┴──────────────┘

我如何做到这一点?

【问题讨论】:

    标签: clickhouse


    【解决方案1】:
    select anyIf(result, rule_name = 'string_1') string_1,  
           anyIf(result, rule_name = 'string_2') string_2,
           anyIf(result, rule_name = 'string_3') string_3,
           anyIf(result, rule_name = 'string_4') string_4
           from (
    select 'string_1' rule_name, 'result_1' result
    union  all select 'string_2', 'result_2'
    union  all select 'string_3', 'result_3'
    union  all select 'string_4', 'result_4')
    
    ┌─string_1─┬─string_2─┬─string_3─┬─string_4─┐
    │ result_1 │ result_2 │ result_3 │ result_4 │
    └──────────┴──────────┴──────────┴──────────┘
    

    【讨论】:

    • 我想把它转成这样 string_1, string_2 ... 是列,结果应该有 4 列和 1 行
    • 如何理解哪一行地址哪一列?
    • 谢谢,第二个查询没有给出正确的结果。我需要这个。 ┌─a────────────────┬─b────────────────┬─c────────────────┬─d────────────────┐ │ string_2 result_2 │ string_3 result_3 │ string_1 result_1 │ string_4 result_4 │ └──────────────────┴──────────────────┴──────────────────┴──────────────────┘ 1 rows in set.
    • 我又添加了一个例子。你的问题真的非常令人困惑。
    • 对不起,造成混乱。这正是我所需要的。
    猜你喜欢
    • 1970-01-01
    • 2011-06-26
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多