Teradata:执行列的枢轴操作值插头

发布于 2025-01-31 05:59:12 字数 2363 浏览 2 评论 0原文

我在teradata上有一个db.tab_unpivoted有数百万的行,而4个 - - <代码> id ,<代码>周,sales&amp; 利润。以下是问题的小缩影。

为了插图,db.tab_unpivoted就是这样:

://i.sstatic.net/n6dq6.png“ alt =“未分明表”>

我想pivot此表。以下是我的代码:

SET SQL_STMT = ('CREATE TABLE DB.TAB_PIVOTED AS
                (   SELECT * FROM DB.TAB_UNPIVOTED
                    PIVOT
                    (   SUM(Sales) AS  Sales, SUM(Profits) AS Profits  
                        FOR KW_Prefix IN (
                                          'CW_1' AS CW_1,
                                          'CW_2' AS CW_2,
                                          'CW_3' AS CW_3
                                         )
                    ) AS dt 
               ) WITH DATA;'
              ) ;
EXECUTE IMMEDIATE SQL_STMT;

奇怪的是,在关键上,每次我得到不同的表db.tab_pivoted,其中跨不同列的值互换。例如一次输出可以是:

”枢纽表“

但是,下次可能是一个结果,值互换,尽管profits&amp; 销售维护配对。在一个输出中,它们可以在cw_1下,在另一个运行中,它可以在cw_3下:

“枢轴表2”

这个问题可能不会在小的小问题上复制数据,但是我的数据以数百万和cw从1到52变化,我一直都在看到。

有人有一个想法吗?在枢纽中,我犯了错误吗?

输入将非常尊敬。

更新:

我也尝试直接运行代码,而不是立即执行SQL_STMT,但结果相同。

CREATE TABLE DB.TAB_PIVOTED AS
                    (   SELECT * FROM DB.TAB_UNPIVOTED
                        PIVOT
                        (   SUM(Sales) AS  Sales, SUM(Profits) AS Profits  
                            FOR KW_Prefix IN (
                                              'CW_1' AS CW_1,
                                              'CW_2' AS CW_2,
                                              'CW_3' AS CW_3
                                             )
                        ) AS dt 
                   ) WITH DATA;

I am having a Table (DB.TAB_UNPIVOTED) on TeraData having millions of rows, and 4 columns - ID, Week, Sales & Profits. Here below is a small microcosm of the problem.

For sake of illustration DB.TAB_UNPIVOTED is like this:

Unpivoted Table

I want to PIVOT this table. Following is my code:

SET SQL_STMT = ('CREATE TABLE DB.TAB_PIVOTED AS
                (   SELECT * FROM DB.TAB_UNPIVOTED
                    PIVOT
                    (   SUM(Sales) AS  Sales, SUM(Profits) AS Profits  
                        FOR KW_Prefix IN (
                                          'CW_1' AS CW_1,
                                          'CW_2' AS CW_2,
                                          'CW_3' AS CW_3
                                         )
                    ) AS dt 
               ) WITH DATA;'
              ) ;
EXECUTE IMMEDIATE SQL_STMT;

The strange thing is that, on PIVOTING, everytime I get a different table DB.TAB_PIVOTED, where values across different columns are interchanged. For eg; one time the output can be:

Pivoted Table

But, next time, it could be a dfferent result, with values interchanged, though Profits & Sales maintain their pairing. In one output they could be under CW_1, where as on another run, it could be under CW_3:

Pivoted Table 2

This problem may not be reproduced on small data, but with my data in millions and CW varing from 1 to 52, I see it all the time.

Does anyone have an idea, where in the pivoting am I making the mistake?

Inputs would be very esteemed.

Update:

I tried running the code directly as well, instead of EXECUTE IMMEDIATE SQL_STMT,but same strange results.

CREATE TABLE DB.TAB_PIVOTED AS
                    (   SELECT * FROM DB.TAB_UNPIVOTED
                        PIVOT
                        (   SUM(Sales) AS  Sales, SUM(Profits) AS Profits  
                            FOR KW_Prefix IN (
                                              'CW_1' AS CW_1,
                                              'CW_2' AS CW_2,
                                              'CW_3' AS CW_3
                                             )
                        ) AS dt 
                   ) WITH DATA;

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文