teradata:在枢轴中的子句中可以从表中获取数据

发布于 2025-01-28 13:01:14 字数 1321 浏览 4 评论 0 原文

我希望从年度数据中提取几个 calender Weeks 。完成此操作后,我想将其旋转,以便每个 ID 都有一行。

我们有一个表 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 not响应,也没有错误。

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;

如果代替(从db.my_cws选择CW)我会使用(15,16,17),那么一切都很好,我会得到枢纽的表格,如上所示。

谁能建议我在哪里犯错? 非常感谢。

I wish to extract a few Calender Weeks from an yearly data. Once that's done, I want to pivot it, so that there is one row for each ID.

We have a table DB.MY_CWs having just one column CW containing the Calender Weeks we are interested in.

The following code extracts the relevant 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;

This Code gives us the output like this:

Initial Output

But, I would like to pivot it so that I get an output like this:

Desired Output

I took the help from code here and ran the following, but TeraData doesn't respond and there is no Error either.

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;

If instead of (SELECT CW FROM DB.MY_CWs) I would have used (15,16,17), then everything works fine and I would have got the pivoted Table, as shown above.

Can anyone suggest where I am making the mistake?
Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风透绣罗衣 2025-02-04 13:01:14

我试图重新创建场景。
我要低于错误。

CREATE TABLE Failed. 4306: (-4306)Invalid PIVOT query: PIVOT query with sub-query in IN-List is not supported in DDL statement.

在枢轴表中使用子查询时几乎没有限制。

TD文档:
https://docs.teradata.com/r/Teradata-VantageTM-NewSQL-Engine-Release-Summary/March-2019/Release-16.20-Feature-Update- 1-features/subquery-support-in-pivot in-list

TD文档的摘要

注意事项

在列表中具有子查询的枢轴在多列表中不支持。枢轴列在优化阶段进行动态决定。由于这种动态行为,以下是枢轴查询的用法考虑因素,并在列表中具有子查询。

  • 在DDL创建语句中不支持。
  • 在存储过程的光标fetch语句中不支持
  • 如果在列表中给出了子查询,则不允许在枢轴查询上进行集合操作。
  • 结果枢轴列名不能在选择列表中明确指定。
  • 不支持子句。

如果您使用的是SQL Assistant,请检查您的历史记录是否有错误详细信息。
否则,您可以查询dbc.dbqlogtbl检查错误文章。

解决方法:

您可以通过动态SQL和存储过程实现所需的输出。

步骤:

  1. 将子查询的输出转换为字符串。我们可以通过xmlagg做到这一点。
  2. 加入in 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();

I tried to recreate the scenario.
I am getting below error.

CREATE TABLE Failed. 4306: (-4306)Invalid PIVOT query: PIVOT query with sub-query in IN-List is not supported in DDL statement.

There are few limitation while using subquery in pivot table.

TD Documentation:
https://docs.teradata.com/r/Teradata-VantageTM-NewSQL-Engine-Release-Summary/March-2019/Release-16.20-Feature-Update-1-Features/Subquery-Support-in-PIVOT-IN-List

Snippet from TD Documentation

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.

If you are using SQL Assistant, kindly check your history for the error details.
Otherwise you can query dbc.dbqlogtbl to check the errortext.

Workaround:

You can achieve the desired output through Dynamic SQL and Stored Procedure.

Steps:

  1. Convert the output of the subquery to a String. We can do that through XMLAGG.
  2. Concatenate the Step1 output in the IN Clause and execute the dynamically generated 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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文