Teradata:Pivot 中的 IN 子句无法从 Table 获取数据
Teradata: IN clause in Pivot can't take data from Table
我希望从年度数据中提取一些 Calender Weeks
。完成后,我想旋转它,以便每个 ID
.
有一行
我们有一个 table DB.MY_CWs
只有一列 CW
包含我们感兴趣的 Calender Weeks
。
以下代码提取相关Calender Weeks
.
CREATE TABLE DB.MY_TABLE AS
(
SELECT ID,
WeekNumber_Of_Year(Sales_Date)) AS CW,
AVG(Sales) AS Sales
FROM DB.DataBase_XYZ
WHERE CW IN (SELECT CW FROM DB.MY_CWs)
GROUP BY ID,CW
) WITH DATA;
此代码为我们提供了如下输出:
但是,我想旋转它以便得到这样的输出:
我从代码 和 运行 获得了以下帮助,但是 TeraData
没有响应,也没有错误。
CREATE TABLE DB.MY_TABLE2 AS
(
SELECT *
FROM DB.MY_TABLE
PIVOT
(SUM(Sales) AS Sales
FOR CW IN (SELECT CW FROM DB.MY_CWs)
) AS dt
) WITH DATA;
如果我使用 (15,16,17)
而不是 (SELECT CW FROM DB.MY_CWs)
,那么一切正常,我会得到旋转的 Table,如上所示。
谁能指出我哪里出错了?
非常感谢。
我试图重现场景。
我遇到错误。
CREATE TABLE Failed. 4306: (-4306)Invalid PIVOT query: PIVOT query with sub-query in IN-List is not supported in DDL statement.
在 pivot 中使用子查询时有一些限制 table。
TD 文档中的片段
Considerations
PIVOT with a subquery in the IN-list is not supported in a multistatement request. PIVOT columns are decided dynamically at the optimization phase. Because of this dynamic behavior, the following are usage considerations of a PIVOT query with a subquery in the IN-list.
- Not supported in DDL creation statements.
- Not supported in stored procedure's cursor FETCH statement.
- SET operations are not allowed on a PIVOT query if subquery is given in the IN-list.
- Resultant PIVOT column names cannot be explicitly specified in the SELECT list.
- Does not support ORDER BY clause.
如果您正在使用 SQL 助手,请检查您的历史记录以了解错误详情。
否则你可以查询 dbc.dbqlogtbl 来检查错误文本。
解决方法:
您可以通过动态 SQL 和存储过程实现所需的输出。
步骤:
- 将子查询的输出转换为字符串。我们可以通过 XMLAGG 做到这一点。
- 在IN子句中拼接Step1的输出,执行动态生成的SQL.
REPLACE PROCEDURE DYNAMIC_PIVOT()
BEGIN
DECLARE Sqltxt VARCHAR(1000);
DECLARE CWtxt VARCHAR(250);
--Convert rows from MY_CWs to comma delimited string
SET CWtxt=(SELECT TRIM( TRAILING ',' FROM ( XMLAGG(CAST(CW AS VARCHAR(10))||',') (VARCHAR(255)) ) ) FROM MY_CWs);
SET Sqltxt=('CREATE TABLE MY_TABLE2 AS
(
SELECT *
FROM MY_TABLE
PIVOT
(SUM(Sales) AS Sales
FOR CW IN ('|| CWtxt ||')
) AS dt
) WITH DATA;') ;
CALL DBC.SYSEXECSQL(Sqltxt);
END;
CALL DYNAMIC_PIVOT();
我希望从年度数据中提取一些 Calender Weeks
。完成后,我想旋转它,以便每个 ID
.
我们有一个 table DB.MY_CWs
只有一列 CW
包含我们感兴趣的 Calender Weeks
。
以下代码提取相关Calender Weeks
.
CREATE TABLE DB.MY_TABLE AS
(
SELECT ID,
WeekNumber_Of_Year(Sales_Date)) AS CW,
AVG(Sales) AS Sales
FROM DB.DataBase_XYZ
WHERE CW IN (SELECT CW FROM DB.MY_CWs)
GROUP BY ID,CW
) WITH DATA;
此代码为我们提供了如下输出:
但是,我想旋转它以便得到这样的输出:
我从代码 TeraData
没有响应,也没有错误。
CREATE TABLE DB.MY_TABLE2 AS
(
SELECT *
FROM DB.MY_TABLE
PIVOT
(SUM(Sales) AS Sales
FOR CW IN (SELECT CW FROM DB.MY_CWs)
) AS dt
) WITH DATA;
如果我使用 (15,16,17)
而不是 (SELECT CW FROM DB.MY_CWs)
,那么一切正常,我会得到旋转的 Table,如上所示。
谁能指出我哪里出错了? 非常感谢。
我试图重现场景。 我遇到错误。
CREATE TABLE Failed. 4306: (-4306)Invalid PIVOT query: PIVOT query with sub-query in IN-List is not supported in DDL statement.
在 pivot 中使用子查询时有一些限制 table。
TD 文档中的片段
Considerations
PIVOT with a subquery in the IN-list is not supported in a multistatement request. PIVOT columns are decided dynamically at the optimization phase. Because of this dynamic behavior, the following are usage considerations of a PIVOT query with a subquery in the IN-list.
- Not supported in DDL creation statements.
- Not supported in stored procedure's cursor FETCH statement.
- SET operations are not allowed on a PIVOT query if subquery is given in the IN-list.
- Resultant PIVOT column names cannot be explicitly specified in the SELECT list.
- Does not support ORDER BY clause.
如果您正在使用 SQL 助手,请检查您的历史记录以了解错误详情。 否则你可以查询 dbc.dbqlogtbl 来检查错误文本。
解决方法:
您可以通过动态 SQL 和存储过程实现所需的输出。
步骤:
- 将子查询的输出转换为字符串。我们可以通过 XMLAGG 做到这一点。
- 在IN子句中拼接Step1的输出,执行动态生成的SQL.
REPLACE PROCEDURE DYNAMIC_PIVOT()
BEGIN
DECLARE Sqltxt VARCHAR(1000);
DECLARE CWtxt VARCHAR(250);
--Convert rows from MY_CWs to comma delimited string
SET CWtxt=(SELECT TRIM( TRAILING ',' FROM ( XMLAGG(CAST(CW AS VARCHAR(10))||',') (VARCHAR(255)) ) ) FROM MY_CWs);
SET Sqltxt=('CREATE TABLE MY_TABLE2 AS
(
SELECT *
FROM MY_TABLE
PIVOT
(SUM(Sales) AS Sales
FOR CW IN ('|| CWtxt ||')
) AS dt
) WITH DATA;') ;
CALL DBC.SYSEXECSQL(Sqltxt);
END;
CALL DYNAMIC_PIVOT();