构建查询以在单行上为同一用户连接多行 (Access 2010)
Build a query to join multiple rows for same user on a single row (Access 2010)
很抱歉,如果这个问题已经得到解答,我搜索了又搜索,但找不到这个确切的场景。
我有 table 个用户,table 个有活动(添加或删除)。我想通过查询将这些组合成一个结果,并将 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
这就是我想要查询的 return
User AddDate RemoveDate
=====================================
John Doe 1/1/2017 2/6/2017
Jane Doe 1/3/2017
John Smith 2/2/2017
今早又新鲜看了一遍,明白了。当我不可避免地忘记一年后的答案并且不得不 Google 寻找答案时发布答案!
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])
很抱歉,如果这个问题已经得到解答,我搜索了又搜索,但找不到这个确切的场景。
我有 table 个用户,table 个有活动(添加或删除)。我想通过查询将这些组合成一个结果,并将 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
这就是我想要查询的 return
User AddDate RemoveDate
=====================================
John Doe 1/1/2017 2/6/2017
Jane Doe 1/3/2017
John Smith 2/2/2017
今早又新鲜看了一遍,明白了。当我不可避免地忘记一年后的答案并且不得不 Google 寻找答案时发布答案!
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])