MS Access:创建排名/优先级并显示最高行
MS Access: Create Ranking/ Priority & Show Highest Row
我正在尝试为我的组织创建一个关于我们的员工和他们为工作持有的相关许可证的数据库。我将 100 多个工作和许可证分为三个桶以尝试对齐它们:1. 普通员工,2. 管理,3. 服务
但是我们有一些问题:
因为许可证也有背书,所以被认为是服务人员但拥有 "general employee" 许可证的人仍然可以从事服务工作(因为我们假设他们有背书在服务业工作)
如果用户的执照和职位可能不匹配,我们不想删除他们。
教育工作者可以在同一组/桶中拥有多个许可证
所以我想为许可证和职位之间的这些关系创建一个排名/优先级(排名见下文)并且我想创建一个查询,以便如果一个人拥有多个许可证(从而产生多个行),我们可以保留顶行,无论它是 1、2 还是 3(通过优先考虑 1s 而不是 2s 而不是 3s),并删除底线。
完美匹配(角色 - 许可):
- 一般 - 一般 = 1
- 服务 - 服务 = 1
- 管理员 - 管理员 = 1
匹配度高:
- 一般 - 服务 = 2
- 服务 - 一般 = 2
不匹配:
- 一般 - 管理员 = 3
- 服务 - 管理员 = 3
- 管理员 - 服务 = 3
- 管理员 - 常规 = 3
这可能吗?
我会通过创建一个未命名的子查询来解决这个问题,我在其中明确说明了 3 个子查询中每个等级的值。然后,我将按排名字段对外部查询进行排序。
下面的查询将所有 1 分组在顶部,然后是 2 和 3。假设你想先按人分组,你会列出任何字段,可以在排序依据中首先唯一地标识这个人。例如:"ORDER BY EmployerNo, Rank"。
此外,如果您不希望排名字段出现在输出中,只需在外部查询中明确说明要 select 的字段,而不是我在下面使用的 "SELECT *" .
SELECT *
FROM(
SELECT 1 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "General")
OR (Role = "Services" and License = "Services")
OR (Role = "Admin" and License = "Admin")
UNION
SELECT 2 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "Services")
OR (Role = "Services" and License = "General")
UNION
SELECT 3 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "Admin")
OR (Role = "Services" and License = "Admin")
OR (Role = "Admin" and License = "Services")
OR (Role = "Admin" and License = "General")
) as a
ORDER BY Rank
编辑:添加请求的变化以仅报告每个员工和许可证的最高排名记录。因此,我加入了之前的查询,并做了一些小改动。所做的更改是使查询成为聚合查询,其中我们仅报告每个许可证和员工的最高等级。 (我在猜测员工字段的名称)。当我们内部加入这些查询时,我们将报告原始查询中与 MaxRank、license# 和 employee# 匹配的所有字段。
SELECT a.*
FROM(
SELECT 1 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "General")
OR (Role = "Services" and License = "Services")
OR (Role = "Admin" and License = "Admin")
UNION
SELECT 2 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "Services")
OR (Role = "Services" and License = "General")
UNION
SELECT 3 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "Admin")
OR (Role = "Services" and License = "Admin")
OR (Role = "Admin" and License = "Services")
OR (Role = "Admin" and License = "General")
) as a
INNER JOIN (
SELECT Min(rank) as maxrank, License, employee
FROM(
SELECT 1 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "General")
OR (Role = "Services" and License = "Services")
OR (Role = "Admin" and License = "Admin")
UNION
SELECT 2 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "Services")
OR (Role = "Services" and License = "General")
UNION
SELECT 3 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "Admin")
OR (Role = "Services" and License = "Admin")
OR (Role = "Admin" and License = "Services")
OR (Role = "Admin" and License = "General")
) as a
GROUP BY License, employee
) as b on a.rank = b.maxrank and a.License = b.License and a.employee = b.employee
ORDER BY Rank
编辑 #2:将最大值更改为最小值
我正在尝试为我的组织创建一个关于我们的员工和他们为工作持有的相关许可证的数据库。我将 100 多个工作和许可证分为三个桶以尝试对齐它们:1. 普通员工,2. 管理,3. 服务
但是我们有一些问题:
因为许可证也有背书,所以被认为是服务人员但拥有 "general employee" 许可证的人仍然可以从事服务工作(因为我们假设他们有背书在服务业工作)
如果用户的执照和职位可能不匹配,我们不想删除他们。
教育工作者可以在同一组/桶中拥有多个许可证
所以我想为许可证和职位之间的这些关系创建一个排名/优先级(排名见下文)并且我想创建一个查询,以便如果一个人拥有多个许可证(从而产生多个行),我们可以保留顶行,无论它是 1、2 还是 3(通过优先考虑 1s 而不是 2s 而不是 3s),并删除底线。
完美匹配(角色 - 许可):
- 一般 - 一般 = 1
- 服务 - 服务 = 1
- 管理员 - 管理员 = 1
匹配度高:
- 一般 - 服务 = 2
- 服务 - 一般 = 2
不匹配:
- 一般 - 管理员 = 3
- 服务 - 管理员 = 3
- 管理员 - 服务 = 3
- 管理员 - 常规 = 3
这可能吗?
我会通过创建一个未命名的子查询来解决这个问题,我在其中明确说明了 3 个子查询中每个等级的值。然后,我将按排名字段对外部查询进行排序。
下面的查询将所有 1 分组在顶部,然后是 2 和 3。假设你想先按人分组,你会列出任何字段,可以在排序依据中首先唯一地标识这个人。例如:"ORDER BY EmployerNo, Rank"。
此外,如果您不希望排名字段出现在输出中,只需在外部查询中明确说明要 select 的字段,而不是我在下面使用的 "SELECT *" .
SELECT *
FROM(
SELECT 1 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "General")
OR (Role = "Services" and License = "Services")
OR (Role = "Admin" and License = "Admin")
UNION
SELECT 2 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "Services")
OR (Role = "Services" and License = "General")
UNION
SELECT 3 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "Admin")
OR (Role = "Services" and License = "Admin")
OR (Role = "Admin" and License = "Services")
OR (Role = "Admin" and License = "General")
) as a
ORDER BY Rank
编辑:添加请求的变化以仅报告每个员工和许可证的最高排名记录。因此,我加入了之前的查询,并做了一些小改动。所做的更改是使查询成为聚合查询,其中我们仅报告每个许可证和员工的最高等级。 (我在猜测员工字段的名称)。当我们内部加入这些查询时,我们将报告原始查询中与 MaxRank、license# 和 employee# 匹配的所有字段。
SELECT a.*
FROM(
SELECT 1 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "General")
OR (Role = "Services" and License = "Services")
OR (Role = "Admin" and License = "Admin")
UNION
SELECT 2 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "Services")
OR (Role = "Services" and License = "General")
UNION
SELECT 3 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "Admin")
OR (Role = "Services" and License = "Admin")
OR (Role = "Admin" and License = "Services")
OR (Role = "Admin" and License = "General")
) as a
INNER JOIN (
SELECT Min(rank) as maxrank, License, employee
FROM(
SELECT 1 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "General")
OR (Role = "Services" and License = "Services")
OR (Role = "Admin" and License = "Admin")
UNION
SELECT 2 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "Services")
OR (Role = "Services" and License = "General")
UNION
SELECT 3 as rank, *
FROM YourTable
WHERE (Role = "General" and License = "Admin")
OR (Role = "Services" and License = "Admin")
OR (Role = "Admin" and License = "Services")
OR (Role = "Admin" and License = "General")
) as a
GROUP BY License, employee
) as b on a.rank = b.maxrank and a.License = b.License and a.employee = b.employee
ORDER BY Rank
编辑 #2:将最大值更改为最小值