具有旋转数据的阶梯行为
向其他程序员和查询编写者致敬,
我有一个漂亮的查询
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
选择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
万一...
Just in CASE...