这是问题所在:
CREATE TABLE #datesX
(
[Name] [varchar] (8) NULL
, [MarriedDate] datetime NULL
, [HappyScoreX] [int] NULL
)
INSERT INTO #datesX
values
('TIM', null, 20),
('RAJ', '01 jan 2012', 20),
('PAUL', '01 jan 1960', 20)
CREATE TABLE #datesY
(
[Name] [varchar] (8) NULL
, [FirstSprogDate] datetime NULL
, [HappyScoreY] [int] NULL
)
INSERT INTO #datesY
values
('TIM', '01 jan 1995', 20),
('RAJ', '20 JUN 2013', 20),
('JASE', null, 20),
('PAUL', '01 jan 1970', 20)
CREATE TABLE #datesZ
(
[Name] [varchar] (8) NULL
, [FoundGodDate] datetime NULL
, [HappyScoreZ] [int] NULL
)
INSERT INTO #datesZ
values
('TIM', null, 20),
('RAJ', null, 20),
('JASE', '01 DEC 2012', 20),
('PAUL', '01 jan 1970', 20)
SELECT
[Name] = COALESCE(x.Name, y.Name, z.Name),
x.[MarriedDate],
y.[FirstSprogDate],
z.[FoundGodDate],
[HappyScoreX] = SUM(ISNULL(x.[HappyScoreX],0.0)),
[HappyScoreY] = SUM(ISNULL(y.[HappyScoreY],0.0)),
[HappyScoreZ] = SUM(ISNULL(z.[HappyScoreZ],0.0))
FROM
#datesX x
FULL OUTER JOIN #datesY y ON
x.Name = y.Name
FULL OUTER JOIN #datesZ z ON
x.Name = z.Name
GROUP BY
COALESCE(x.Name, y.Name, z.Name),
x.[MarriedDate],
y.[FirstSprogDate],
z.[FoundGodDate]
我可以看到为什么记录JASE
被重复 - 我如何修改查询以便JASE
只有一条记录?