在将一个pivot sql嵌套到另一个sql中时,我得到了 ORA-00913:"太多的值" 错误。
我如何纠正这一点,或者是否有一种更简单的方式来写这个,因为我需要" NEEDTHISFIELD"另一个项目的领域。
SELECT null as NEEDTHISFIELD, (SELECT * FROM(
SELECT th.BUCHDAT,th.OID,th.EFTYP,ef.NAME,thp.XORDER,thp.THEVALUE
FROM THISTORY th
LEFT JOIN EFTYP ef on th.EFTYP = ef.OID
LEFT JOIN THPARA thp on th.OID = thp.THISTORY
WHERE th.EFTYP = 'xyz'
)
PIVOT (MAX(THEVALUE) FOR (XORDER) IN (
'100' AS "VAL1",
'101' AS "VAL2"
)))
FROM DUAL;
非常感谢!
答案 0 :(得分:2)
您的嵌套查询返回多一条记录。 试试这样的事情
SELECT null as NEEDTHISFIELD,t.* FROM(
SELECT th.BUCHDAT,th.OID,th.EFTYP,ef.NAME,thp.XORDER,thp.THEVALUE
FROM THISTORY th
LEFT JOIN EFTYP ef on th.EFTYP = ef.OID
LEFT JOIN THPARA thp on th.OID = thp.THISTORY
WHERE th.EFTYP = 'xyz'
)
PIVOT (MAX(THEVALUE) FOR (XORDER) IN (
'100' AS "VAL1",
'101' AS "VAL2"
) t ;
答案 1 :(得分:2)
只需包含透视表的别名,然后您可以将所需字段添加到所选字段列表中:
SELECT t.*,
NULL AS NeedThisField
FROM ( SELECT th.BUCHDAT,
th.OID,
th.EFTYP,
ef.NAME,
thp.XORDER,
thp.THEVALUE
FROM THISTORY th
LEFT JOIN EFTYP ef on th.EFTYP = ef.OID
LEFT JOIN THPARA thp on th.OID = thp.THISTORY
WHERE th.EFTYP = 'xyz'
)
PIVOT (
MAX(THEVALUE)
FOR (XORDER) IN ( '100' AS "VAL1", '101' AS "VAL2" )
) t;