我的数据看起来像这样;
Id Name Number Birthdate
0 Dajit Singh NULL 6/9/2007
5 Dajit Singh 12345 NULL
11 Dajit Singh NULL NULL
我想以编程方式将这些行组合成一行,并提供所有可用数据:
0 Dajit Singh 12345 6/9/2007
这是我到目前为止所做的,但它没有做到:
Select Id,
Max(Name) As Name,
Max(Number) As Number,
Max(Birthdate) As Birthdate
From tblPerson
Group By Id, Name, Number, Birthdate
Order By Name
如何实现这一目标?
答案 0 :(得分:1)
请检查脚本:
DECLARE @table TABLE
(
Id INT NOT NULL,
Name VARCHAR(50) NOT NULL,
Number INT NULL,
Birthdate DATETIME NULL
)
INSERT INTO @table(Id, Name, Number, Birthdate) VALUES
(0, 'Dajit Singh', NULL, '20070906'),
(5, 'Dajit Singh', 12345, NULL),
(11, 'Dajit Singh', NULL, NULL),
(100, 'Alex', NULL, '20080906'),
(500, 'Alex', 12345, NULL),
(1100, 'Alex', NULL, NULL)
SELECT
MIN(Id) As Id,
Name As Name,
MAX(Number) As Number,
MAX(Birthdate) As Birthdate
FROM @table
GROUP BY Name
ORDER BY Name