我不仅仅是Access 2010的新手,但肯定不是专家。我每天都使用Access,但这个问题让我很难过。
我有一个表,我需要查询以检索一组组,子组和所有可能的子成员。 该表格如下:
期望的GROUP |当前TN | SPRINT TELEPHONE号码|最后一个名字|第一个名称
5553215255 | 5553210001 | 4143210009 |威尔鲍勃
5553215255 | 5553210002 | 4143210008 |加西亚杰瑞
5553215255 | 5553210003 | 4143210007 | Hendrix Jimi
5553215255 | 5553210004 | 4143210006 | Peds页面
5553210004 | 3144230009 | 4143210005 |现金约翰尼
5553210004 | 3144230008 | 4143210003 |莫里森吉姆
5553210004 | 3144230007 | 4143210002 |贝多芬路德维希
5553210004 | 3144230911 | 4143210001 |紧急Peds页面
3144230911 | 6362341101 | 4143210099 | Strummer Joe
3144230911 | 6362341101 | 4143210098 |水星弗雷迪郎
3144230911 | 6362341101 | 4143210097 |普雷斯利埃尔维斯
3144230911 | 6362341101 | 4143210096 | Bopper Big
字段定义:
我需要一个查询,当运行时返回所有分页组成员的列表。 假设我向医生发送了一个页面。寻呼医生还会寻呼Peds的所有成员,然后将所有紧急求助成员分页。结果将显示所有医生成员,Peds的所有成员(除了组名称)以及因此而被分页的所有紧急求助成员的初始页面。注意Peds页面和紧急Peds页面被忽略了。插图如下:
期望的GROUP |当前TN | SPRINT TELEPHONE号码|最后一个名字|第一个名称
5553215255 | 5553210001 | 4143210009 |威尔鲍勃
5553215255 | 5553210002 | 4143210008 |加西亚杰瑞
5553215255 | 5553210003 | 4143210007 | Hendrix Jimi
5553210004 | 3144230009 | 4143210005 |现金约翰尼
5553210004 | 3144230008 | 4143210003 |莫里森吉姆
5553210004 | 3144230007 | 4143210002 |贝多芬路德维希
3144230911 | 6362341101 | 4143210099 | Strummer Joe
3144230911 | 6362341101 | 4143210098 |水星弗雷迪郎
3144230911 | 6362341101 | 4143210097 |普雷斯利埃尔维斯
3144230911 | 6362341101 | 4143210096 | Bopper Big
MIGHTY BIG先谢谢。
编辑以包含来自评论的sql。
SELECT *
FROM (SELECT tbl_group_build.[desired group],tbl_group_build.[current tn],
tbl_group_build.[sprint telephone number],
tbl_group_build.[last name],
tbl_group_build.[first name]
FROM tbl_group_build
UNION
SELECT tbl_Group_build_1.[desired group],tbl_Group_build_1.[current tn],
tbl_Group_build_1.[sprint telephone number],
tbl_Group_build_1.[last name],tbl_Group_build_1.[first name]
FROM tbl_group_build
INNER JOIN tbl_group_build AS tbl_Group_build_1
ON tbl_group_build.[current tn] =
tbl_Group_build_1.[desired group]
UNION
SELECT tbl_Group_build_2.[desired group],tbl_Group_build_2.[current tn],
tbl_Group_build_2.[sprint telephone number],
tbl_Group_build_2.[last name],tbl_Group_build_2.[first name]
FROM (tbl_group_build
INNER JOIN tbl_group_build AS tbl_Group_build_1
ON tbl_group_build.[current tn] =
tbl_Group_build_1.[desired group])
INNER JOIN tbl_group_build AS tbl_Group_build_2
ON tbl_Group_build_1.[current tn] =
tbl_Group_build_2.[desired group]) g
答案 0 :(得分:3)
这是一个想法的开始。我注意到在样本数据中,组长的电话号码与页面的电话号码相同。还有其他可能的联接,但这是迄今为止最干净的。
SELECT *
FROM (SELECT groups.group_name,
groups.[group_lead member],
groups.tn,
groups.last_name,
groups.first_name
FROM groups
WHERE groups.group_name = [enter page]
UNION
SELECT Groups_1.group_name,
Groups_1.[group_lead member],
Groups_1.tn,
Groups_1.last_name,
Groups_1.first_name
FROM groups
INNER JOIN groups AS Groups_1
ON groups.tn = Groups_1.[group_lead member]
UNION
SELECT Groups_2.group_name,
Groups_2.[group_lead member],
Groups_2.tn,
Groups_2.last_name,
Groups_2.first_name
FROM (groups
INNER JOIN groups AS Groups_1
ON groups.tn = Groups_1.[group_lead member])
INNER JOIN groups AS Groups_2
ON Groups_1.tn = Groups_2.[group_lead member]) g
WHERE Trim(Replace([last_name], "page", ""))
NOT IN (SELECT DISTINCT group_name FROM groups)
新数据
SELECT *
FROM (SELECT groups.[desired group],groups.[current tn],
groups.[sprint telephone number],
groups.[last name],groups.[first name]
FROM groups
WHERE groups.[desired group] = [enter desired group]
UNION
SELECT Groups_1.[desired group],Groups_1.[current tn],
Groups_1.[sprint telephone number],Groups_1.[last name],
Groups_1.[first name]
FROM groups
INNER JOIN groups AS Groups_1
ON groups.[current tn] = Groups_1.[desired group]
UNION
SELECT Groups_2.[desired group],Groups_2.[current tn],
Groups_2.[sprint telephone number],Groups_2.[last name],
Groups_2.[first name]
FROM (groups
INNER JOIN groups AS Groups_1
ON groups.[current tn] = Groups_1.[desired group])
INNER JOIN groups AS Groups_2
ON Groups_1.[current tn] = Groups_2.[desired group]) AS g
WHERE g.[last name] NOT IN (SELECT [last name]
FROM groups
WHERE [current tn] IN (SELECT DISTINCT
[desired group]
FROM groups))