【问题标题】:How put transform rows to column in SQL如何在 SQL 中将转换行转换为列
【发布时间】:2021-06-23 11:44:24
【问题描述】:

我有下表

EURtoUSD 列始终为car 中不同组的所有日期显示相同的值。

如何创建下表?

【问题讨论】:

    标签: sql vertica


    【解决方案1】:

    使用条件聚合:

    select date, eurtousd,
           max(case when car = 'Audi' then priceeur end) as audi,
           max(case when car = 'BMW' then priceeur end) as bmw,
           max(case when car = 'MB' then priceeur end) as mb
    from t
    group by date, eurtousd;
    

    【讨论】:

    • 谢谢。如果我想再次从宽表(第 2 表)创建长表(第 1 表),您也可以帮忙,我应该使用什么?
    • @PriitMets 。 . .你可以使用union all
    • 这会使查询过长(实际情况下有更多汽车)。有没有类似 UNPIVOT (Oracle) 功能的东西?
    • 恐怕 Vertica 没有枢轴或取消枢轴功能。我通常的解决方法是使用 SQL 生成 SQL 来,是的,生成执行枢轴的 SQL 语句。
    猜你喜欢
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多