我希望从年度数据中提取几个 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:

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

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.
发布评论
评论(1)
我试图重新创建场景。
我要低于错误。
在枢轴表中使用子查询时几乎没有限制。
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文档的摘要
如果您使用的是SQL Assistant,请检查您的历史记录是否有错误详细信息。
否则,您可以查询dbc.dbqlogtbl检查错误文章。
解决方法:
您可以通过动态SQL和存储过程实现所需的输出。
步骤:
I tried to recreate the scenario.
I am getting below error.
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
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: