条件联合并将行编辑为列

Conditional union and edit rows as columns

到目前为止,我一直对我在这个网站上的发现印象深刻。这次轮到我提问了...

我有以下表格:

预约:

ApptId ResourceId
12345 abcde
12345 bcdef
23456 defgh
34567 cdefg

资源:

ResourceId ResourceType
abcde Person
bcdef Place
cdefg Place
defgh Person

地点:

PlaceId ResourceId PlaceName
zzzzz bcdef Boston
yyyyy cdefg Dallas

人:

PersonId ResourceId PersonName
wwwww abcde Smith
xxxxx defgh Doe

我试着拥有这个:

ApptId ResourceName PlaceName
12345 Smith Boston
23456 Doe
34567 Dallas

所以目前我可以拥有这个:

ApptId ResourceName PlaceName
12345 Smith
12345 Boston
23456 Doe
34567 Dallas

但是如果 Resource 和 Place 是同一个 Appointment,我无法聚合它们。

希望这是清楚的

您可能希望 SELECT 语句包含两个左外连接,如下所示:

SELECT Appt.ApptId, Person.PersonName, Place.PlaceName
FROM Appt
LEFT OUTER JOIN Person ON Appt.ResourceId = Person.ResourceId
LEFT OUTER JOIN Place ON Appt.ResourceId = Place.ResourceId