我的目标是在Entity
表上为包含2个人的所有家庭计算和设置HouseholdName字段。我正在使用SQL Server 2012,我必须使用的模式是:
CREATE TABLE Entity
(
EID INT, --PK
PID INT, --FK to Person.[Person ID]
HouseholdID VARCHAR(50),
HouseholdName VARCHAR(300)
);
CREATE TABLE Person
(
[Person ID] INT,
FirstName VARCHAR(30),
LastName VARCHAR(30)
);
INSERT INTO Entity VALUES (1,1,'HH1',NULL);
INSERT INTO Entity VALUES (2,2,'HH1',NULL);
INSERT INTO Entity VALUES (3,3,'HH2',NULL);
INSERT INTO Entity VALUES (4,4,'HH2',NULL);
INSERT INTO Entity VALUES (5,5,'HH3',NULL);
INSERT INTO Person VALUES (1,'Jane', 'Doe');
INSERT INTO Person VALUES (2,'Joe', 'Doe');
INSERT INTO Person VALUES (3,'Sara', 'Dillin');
INSERT INTO Person VALUES (4,'Bill', 'Smith');
INSERT INTO Person VALUES (5,'Ted', 'Loner');
计算家喻户晓的逻辑是这样的:
如果两个姓氏相同,例如对于HouseholdID HH1
,则家喻户晓的姓名应为'Jane& Joe Doe'。如果姓氏不同,例如在HH2
中,它的格式应为'Sara Dillin&比尔史密斯'
我可以识别这些人并按顺序得到他们:
SELECT FirstName,LastName,HouseholdID
FROM Entity e
JOIN Person p ON p.[Person ID]=e.[PID]
WHERE HouseholdID IN --HHs of 2 people
(
SELECT DISTINCT HouseholdID
FROM Entity e
JOIN Person p ON p.[Person ID]=e.[PID]
GROUP BY HouseholdID HAVING COUNT(*)=2
)
ORDER BY HouseholdID
但是我坚持过去了。从像C#这样的过程语言我知道如何解决这个问题,但它很慢。我希望能够以更快的SQL方式完成它。
答案 0 :(得分:1)
抱歉,我稍微更改了字段名称,以便于编码。以下查询将为您提供所需内容:
SELECT
e1.HouseholdID,
CASE WHEN p2.LastName = p1.LastName THEN p1.FirstName + ' & ' + p2.FirstName + ' ' + p2.LastName
ELSE p1.FirstName + ' ' + p1.LastName + ' & ' + p2.FirstName + ' ' + p2.LastName END AS HouseholdName,
p1.FirstName,
p1.LastName,
p2.FirstName as p2FirstName,
p2.LastName as p2LastName
FROM
Person p1
INNER JOIN Entity e1 ON
p1.id = e1.PID
LEFT JOIN Entity e2
ON e1.HouseholdID = e2.HouseholdID AND e1.id < e2.id
LEFT JOIN Person p2
ON e2.PID = p2.id
WHERE e2.ID IS NOT NULL
AND NOT EXISTS
( SELECT * FROM Entity e3
WHERE e1.HouseholdID = e3.HouseholdID
AND NOT e3.id IN (e1.id, ISNULL(e2.id, 0))
)