将不同列中的值移动到一行
Move values in different columns to one row
我有以下 table:
CREATE TABLE dbo.Examples
(
Id int not null,
row1 nvarchar(50) null,
row2 nvarchar(50) null,
row3 int null
)
使用此示例数据:
INSERT INTO Examples (id, row1, row2, row3)
VALUES (1, 'Value1', NULL, NULL),
(1, NULL, 'Value1', NULL),
(1, NULL, NULL, 28)
Id
是我的主键,这个在table中应该是唯一的。这对于我的所有行都是可能的,如果我在上面的示例中简单地将所有值移动到一行。
我只需要在数据看起来像上面示例中的那样时使它起作用。这意味着期望的结果将如下所示:
VALUES (1, 'Value1', 'Value1', 28)
一行,所有值都移到一行。
我该如何完成?
您可以使用简单聚合,这对您有用吗?
select id, Max(row1) row1, Max(row2) row2, Max(row3) row3
from examples
group by id
我有以下 table:
CREATE TABLE dbo.Examples
(
Id int not null,
row1 nvarchar(50) null,
row2 nvarchar(50) null,
row3 int null
)
使用此示例数据:
INSERT INTO Examples (id, row1, row2, row3)
VALUES (1, 'Value1', NULL, NULL),
(1, NULL, 'Value1', NULL),
(1, NULL, NULL, 28)
Id
是我的主键,这个在table中应该是唯一的。这对于我的所有行都是可能的,如果我在上面的示例中简单地将所有值移动到一行。
我只需要在数据看起来像上面示例中的那样时使它起作用。这意味着期望的结果将如下所示:
VALUES (1, 'Value1', 'Value1', 28)
一行,所有值都移到一行。
我该如何完成?
您可以使用简单聚合,这对您有用吗?
select id, Max(row1) row1, Max(row2) row2, Max(row3) row3
from examples
group by id