【问题标题】:Turn a single row in SQL into a column将 SQL 中的单行转换为列
【发布时间】:2021-03-29 22:59:48
【问题描述】:

我有这张桌子

dai_total_sum usdc_balance_usd uni_balance_usd comp_balance_usd
54584359.783183105 59754640.523722 15818895.098874114 6035764.061094895

我想把它变成这张桌子

Currency usd_value
DAI 54584359.783183105
USDC 59754640.523722
UNI 15818895.098874114
COMP 6035764.061094895

我尝试过使用 unpivot,但大多数其他示例都依赖于具有多行的数据集。但既然我只有一行,我将如何完成这项任务。

感谢任何人提供的任何帮助。

【问题讨论】:

  • 如果你的表有 100 行怎么办?那你的预期结果是什么?

标签: mysql sql dataframe transform


【解决方案1】:

最简单的是:

select 'DAI' as Currency, dai_total_sum as usd_value
from table1
union all
:
:
union all
select 'COMP' as Currency, comp_balance_usd as usd_value
from table1

【讨论】:

    猜你喜欢
    • 2020-07-01
    • 1970-01-01
    • 2020-04-28
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多