Sql 将数据库记录合并为1个记录

Sql 将数据库记录合并为1个记录,sql,sql-server,tsql,Sql,Sql Server,Tsql,我想把这两行合并成一行,不留下空字段 我正在使用MS SQL Server 2016: MS SQL Server 2017架构设置: CREATE TABLE MyTable ( ActivityId int, CaseId int, Description varchar(255), Actions varchar(255) ); INSERT INTO MyTable (ActivityId,CaseId,Description,Actions

我想把这两行合并成一行,不留下空字段

我正在使用MS SQL Server 2016:

MS SQL Server 2017架构设置

CREATE TABLE MyTable (
     ActivityId int,
     CaseId int, 
     Description varchar(255),
     Actions varchar(255)
);

INSERT INTO MyTable (ActivityId,CaseId,Description,Actions) VALUES 
(229189,35057152,NULL,'DELETED'),
(229189,NULL,'DO','DELETED');
SELECT 
     Max(ActivityId) AS ActivityId,
     Max(CaseId) AS CaseId,
     Max(Description) AS Description,
     Max(Actions) AS Actions 
FROM MyTable
GROUP BY ActivityId;
| ActivityId |   CaseId | Description | Actions |
|------------|----------|-------------|---------|
|     229189 | 35057152 |          DO | DELETED |
查询1

CREATE TABLE MyTable (
     ActivityId int,
     CaseId int, 
     Description varchar(255),
     Actions varchar(255)
);

INSERT INTO MyTable (ActivityId,CaseId,Description,Actions) VALUES 
(229189,35057152,NULL,'DELETED'),
(229189,NULL,'DO','DELETED');
SELECT 
     Max(ActivityId) AS ActivityId,
     Max(CaseId) AS CaseId,
     Max(Description) AS Description,
     Max(Actions) AS Actions 
FROM MyTable
GROUP BY ActivityId;
| ActivityId |   CaseId | Description | Actions |
|------------|----------|-------------|---------|
|     229189 | 35057152 |          DO | DELETED |

CREATE TABLE MyTable (
     ActivityId int,
     CaseId int, 
     Description varchar(255),
     Actions varchar(255)
);

INSERT INTO MyTable (ActivityId,CaseId,Description,Actions) VALUES 
(229189,35057152,NULL,'DELETED'),
(229189,NULL,'DO','DELETED');
SELECT 
     Max(ActivityId) AS ActivityId,
     Max(CaseId) AS CaseId,
     Max(Description) AS Description,
     Max(Actions) AS Actions 
FROM MyTable
GROUP BY ActivityId;
| ActivityId |   CaseId | Description | Actions |
|------------|----------|-------------|---------|
|     229189 | 35057152 |          DO | DELETED |

用于查看,或通过物理方式将表中的2行合并为1?用于查看,因此。。。分组并使用max…你怎么能使用max description?就像你做其他事情一样<代码>最大值(说明)。。。因此,如果一条记录为空/空,则另一个值将是最大值,并将被选中。如果数据真的如所描述的那样,那应该是可行的。但是如果在替代行中填写描述或caseId。。。那么。。这需要更多的技巧…我会将ActivityId和动作保留在最大值之外,但会将它们用于group by。OP中不清楚的是,行动是否可以或将有所不同,以及/或在这些情况下该怎么做。不幸的是,给出的样本数据太窄。