具有旋转数据的阶梯行为

发布于 2024-09-26 04:06:55 字数 906 浏览 3 评论 0原文

向其他程序员和查询编写者致敬,

我有一个漂亮的查询

SELECT ID, [1] AS coL1, [15] AS coL2, [2] AS coL3, [16] AS coL4, [12] AS coL5
FROM MY_TABLE
PIVOT (sum(INT_VALUE) FOR FUND_CODE IN ([1],[2],[15],[16],[12])) AS p
--GROUP BY ID, [1] , [15]  , [2] , [16] , [12] 
ORDER BY ID ASC

,它返回如下数据:

10001      182       NULL      NULL      NULL
10001      NULL      81        NULL      NULL
10001      NULL      NULL      182       NULL
10001      NULL      NULL      NULL      81
10002      165       NULL      NULL      NULL
10002      NULL      73        NULL      NULL
10002      NULL      NULL      165       NULL
10002      NULL      NULL      NULL      73

10001 和 10002 是两个主键,我想像这样显示我的数据:

10001      182       81      182      81
10002      165       73      165      73

我尝试将此评论 GROUP BY 为 no有用。 有什么提示吗?它涉及 COALESCE 吗?

Hail to the fellow programmers and query writers,

I have this beautiful query

SELECT ID, [1] AS coL1, [15] AS coL2, [2] AS coL3, [16] AS coL4, [12] AS coL5
FROM MY_TABLE
PIVOT (sum(INT_VALUE) FOR FUND_CODE IN ([1],[2],[15],[16],[12])) AS p
--GROUP BY ID, [1] , [15]  , [2] , [16] , [12] 
ORDER BY ID ASC

That returns me data like this:

10001      182       NULL      NULL      NULL
10001      NULL      81        NULL      NULL
10001      NULL      NULL      182       NULL
10001      NULL      NULL      NULL      81
10002      165       NULL      NULL      NULL
10002      NULL      73        NULL      NULL
10002      NULL      NULL      165       NULL
10002      NULL      NULL      NULL      73

The 10001 and 10002 are two primary keys, and I'd like to show my data like this:

10001      182       81      182      81
10002      165       73      165      73

I tried this commented GROUP BY to no avail.
Any hints? Does it involve COALESCE?

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

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

发布评论

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

评论(2

眼眸印温柔 2024-10-03 04:06:55

选择ID
,SUM(1) AS 'col1'
,SUM(15) AS 'col2'
,SUM(2) AS 'col3'
,SUM(16) AS 'col4'
,SUM(12) AS 'col5'
FROM 表 GROUP BY ID

SELECT ID
,SUM(1) AS 'col1'
,SUM(15) AS 'col2'
,SUM(2) AS 'col3'
,SUM(16) AS 'col4'
,SUM(12) AS 'col5'
FROM Table GROUP BY ID

守不住的情 2024-10-03 04:06:55

万一...

SELECT ID,
SUM(CASE WHEN FUND_CODE = 1 THEN VR_MOVIMENTACAO_QUOTA ELSE 0 END) coL1,
SUM(CASE WHEN FUND_CODE = 15 THEN VR_MOVIMENTACAO_QUOTA ELSE 0 END) coL2,
SUM(CASE WHEN FUND_CODE = 2 THEN VR_MOVIMENTACAO_QUOTA ELSE 0 END) coL3,
SUM(CASE WHEN FUND_CODE = 16 THEN VR_MOVIMENTACAO_QUOTA ELSE 0 END) coL4,
SUM(CASE WHEN FUND_CODE = 12 THEN VR_MOVIMENTACAO_QUOTA ELSE 0 END) coL5

FROM MY_TABLE
GROUP BY ID

Just in CASE...

SELECT ID,
SUM(CASE WHEN FUND_CODE = 1 THEN VR_MOVIMENTACAO_QUOTA ELSE 0 END) coL1,
SUM(CASE WHEN FUND_CODE = 15 THEN VR_MOVIMENTACAO_QUOTA ELSE 0 END) coL2,
SUM(CASE WHEN FUND_CODE = 2 THEN VR_MOVIMENTACAO_QUOTA ELSE 0 END) coL3,
SUM(CASE WHEN FUND_CODE = 16 THEN VR_MOVIMENTACAO_QUOTA ELSE 0 END) coL4,
SUM(CASE WHEN FUND_CODE = 12 THEN VR_MOVIMENTACAO_QUOTA ELSE 0 END) coL5

FROM MY_TABLE
GROUP BY ID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文