按代码分组而不是空列或以上(SQL中的分区)

时间:2017-10-26 20:02:08

标签: sql sql-server

在SQL Server数据库中,我有一个我有兴趣分组的值表

当我做

min(PDM_Objective) over(partition by PDM_Objective)

但永远不会变成小组

所以,我有这个表

CODE|PDM_Objec  |Real_Market|Card_Market
----|-----------|-----------|---
1   |   52.99   |NULL       |NULL
1   |   NULL    |223947.00  |NULL
1001|   44.44   |NULL       |NULL
1001|   NULL    |220519.99  |NULL

转入此

CODE|PDM_Objec  |Real_Market|Card_Market
----|-----------|-----------|---
1   |   52.99   |223947.00  |NULL
1001|   44.44   |220519.99  |NULL

3 个答案:

答案 0 :(得分:0)

你走了:

DECLARE @T TABLE (CODE INT, PDM_Objec DECIMAL(4,2), Real_Market DECIMAL(8,2), Card_Market INT);

INSERT INTO @T VALUES
(1   ,52.99, NULL     , NULL),
(1   ,NULL , 223947.00, NULL),
(1001,44.44, NULL     , NULL),
(1001,NULL , 220519.99, NULL);

SELECT CODE, MAX(PDM_Objec), MAX(Real_Market), MAX(Card_Market)
FROM @T
GROUP BY CODE;

<强> Demo

答案 1 :(得分:0)

怎么样

select CODE
,sum(isnull(PDM_Objec,0))PDM_Objec 
,sum(isnull(Real_Market,0))Real_Market
,sum(isnull(Card_Market,0))Card_Market
from table 
group by CODE

答案 2 :(得分:0)

您可以尝试此查询:

select CODE, sum(isnull(PDM_Objec,0)) PDM_Objec, 
sum(isnull(Real_Market,0)) Real_Market, max(Card_Market) Card_Market
from yourtable group by CODE