尝试在 Oracle SQL 中取消透视列时出错

Posted

技术标签:

【中文标题】尝试在 Oracle SQL 中取消透视列时出错【英文标题】:Getting error when trying to unpivot columns in Oracle SQL 【发布时间】:2019-12-17 16:08:34 【问题描述】:

我通过每个表中的同一列(称为 fema_wo)将多个表连接在一起。我想将所有选定的列放在一个列中,然后在另一列中说明它来自哪个表(基本上与 R 的 pivot_longer() 相同)。当我尝试运行它时,我收到错误No more data to read from socketVendor Code 17410 Error at line: 1; Oracle 的运行版本CORE 11.2.0.4.0 Production

有人知道为什么会发生这种情况以及可能的修复/建议吗?

在此处查看代码:


select * from (

  select dpd.objectid as id1, ss.objectid as id2, ohp.objectid as id3 , ohs.objectid as id4, ugp.objectid as id5, ugs.objectid as id6,
  s.objectid as id7, t.objectid as id8
  from mvdynamicprotectivedevice dpd
  FULL OUTER JOIN mvsupportstructure ss ON (dpd.fema_wo = ss.fema_wo)
  FULL OUTER JOIN mvoverheadprimaryline ohp ON (dpd.fema_wo = ohp.fema_wo)
  FULL OUTER JOIN overheadsecondaryline ohs ON (dpd.fema_wo = ohs.fema_wo)
  FULL OUTER JOIN mvundergroundprimaryline ugp ON (dpd.fema_wo = ugp.fema_wo)
  FULL OUTER JOIN undergroundsecondaryline ugs ON (dpd.fema_wo = ugs.fema_wo)
  FULL OUTER JOIN mvswitch s ON (dpd.fema_wo = s.fema_wo)
  FULL OUTER JOIN mvtransformer t ON (dpd.fema_wo = t.fema_wo)
  WHERE dpd.fema_wo = 'WBS.F.81109.1.1.2'

)
unpivot (

  objectid
  for objectid
  in (id1, id2, id3, id4, id5, id6, id7, id8)

    );

【问题讨论】:

【参考方案1】:

UNION ALL 似乎更简单:

select dpd.objectid as id,
from mvdynamicprotectivedevice dpd
where dpd.fema_wo = 'WBS.F.81109.1.1.2'
union all
select ss.objectid
from mvsupportstructure ss join
     mvdynamicprotectivedevice dpd
     on dpd.fema_wo = ss.fema_wo and dpd.fema_wo = 'WBS.F.81109.1.1.2'
union all
select ohp.objectid
from mvoverheadprimaryline ohp join
     mvdynamicprotectivedevice dpd
     on dpd.fema_wo = ohp.fema_wo and dpd.fema_wo = 'WBS.F.81109.1.1.2'
. . . ;

我的猜测是,由于每个表中有多个匹配项,您的外连接大大增加了行数。

【讨论】:

哦,这太棒了,我完全误解了union all 所做的事情。不过我没有提到,我需要在查询中包含大约 8 个其他字段;这就是我使用FULL OUTER JOIN 的原因,因为某些工作订单上可能没有所有功能,在这种情况下,我希望为这些功能返回空值。

以上是关于尝试在 Oracle SQL 中取消透视列时出错的主要内容,如果未能解决你的问题,请参考以下文章

为啥我们在 SQL Server 中透视文本列时使用 Max 函数?

SQL ORACLE 取消透视

oracle SQL - 取消透视

如何在 Oracle 11 中取消透视单行?

尝试在 SQL Plus 查询中小写列时出错

尝试从 MySql 中的不同表中选择列时出错