You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unpivot表查询操作受阻,寻求正确实现方案

修复Unpivot查询的问题

你的Unpivot语法存在两个核心问题,调整后即可正常执行:

  • IN子句未关联源表实际列
    你直接写了类型名称字符串,但Unpivot需要明确指定每个类型对应的源表列组合。假设你的原表unpivot2结构是每个类型对应两列(比如Dragon_legendary、Dragon_count、Psychic_legendary、Psychic_count、Fire_legendary、Fire_count),需要把这些列按类型分组。

  • 多列Unpivot的语法格式错误
    多列Unpivot时,要把每个类型的列组合用括号包裹,再通过AS指定该组合对应的type1值。

修复后的SQL代码:

SELECT type1, legendary, count
FROM unpivot2
UNPIVOT (
  (legendary, count)
  FOR type1 IN (
    (Dragon_legendary, Dragon_count) AS 'Dragon',
    (Psychic_legendary, Psychic_count) AS 'Psychic',
    (Fire_legendary, Fire_count) AS 'Fire'
  )
) AS s;

如果你的原表列名不是xxx_legendary、xxx_count这种格式,只需把括号里的列名替换成你实际的列名即可。

内容的提问来源于stack exchange,提问作者Rohan Dhoundiyal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 02:31:15