如果已经回答道歉,我搜索并搜索但找不到这个确切的场景。
我有一个用户表和一个包含活动的表(添加或删除)。我想通过查询将这些结合到一个结果中,将AddDate和RemoveDate作为查询中的列。
以下是我的数据示例,以及我正在寻找的内容。
Users
==============
ID User
--------------
1 John Doe
2 Jane Doe
3 John Smith
Activities
===========================================
ID UserID ActivityType ActivityDate
-------------------------------------------
1 1 Add 1/1/2017
2 2 Add 1/3/2017
3 3 Add 2/2/2017
4 1 Remove 2/6/2017
这就是我要回复的问题
User AddDate RemoveDate
=====================================
John Doe 1/1/2017 2/6/2017
Jane Doe 1/3/2017
John Smith 2/2/2017
答案 0 :(得分:0)
今天早上再次看完它后,我想通了。发布答案的时候我不可避免地会忘记一年后的问题,并且必须向谷歌寻求答案!
SELECT [User],
[AddDate],
[RemoveDate]
FROM (( [Users]
LEFT JOIN ( SELECT [UserID],
[ActivityDate] AS AddDate
FROM [Activities]
WHERE [ActivityType] = "Add" ) AS [A1]
ON [Users].[ID] = [A1].[UserID])
LEFT JOIN ( SELECT [UserID],
[ActivityDate] AS RemoveDate
FROM [Activities]
WHERE [ActivityType] = "Remove" ) AS [A2]
ON [Users].[ID] = [A2].[UserID])