Sql MS访问:创建排名/优先级并显示最高行

Sql MS访问:创建排名/优先级并显示最高行,sql,ms-access,ms-access-2013,Sql,Ms Access,Ms Access 2013,我正在尝试为我的组织创建一个关于我们员工及其工作相关许可证的数据库。我将100多份工作和许可证分为三个部分,试图使它们保持一致:1。一般雇员2名。行政,3。服务 然而,我们有几个问题: 因为许可证也有背书,所以被视为服务人员但拥有普通员工许可证的人仍然可以在服务中工作,因为我们假设他们有背书在服务中工作 如果用户的许可证和位置可能不匹配,我们不希望删除用户 教育者可以在同一组/组中拥有多个许可证 因此,我想为这些许可证和职位之间的关系创建一个排名/优先级,请参见下面的排名,我想创建一个查询,这样

我正在尝试为我的组织创建一个关于我们员工及其工作相关许可证的数据库。我将100多份工作和许可证分为三个部分,试图使它们保持一致:1。一般雇员2名。行政,3。服务

然而,我们有几个问题:

因为许可证也有背书,所以被视为服务人员但拥有普通员工许可证的人仍然可以在服务中工作,因为我们假设他们有背书在服务中工作

如果用户的许可证和位置可能不匹配,我们不希望删除用户

教育者可以在同一组/组中拥有多个许可证

因此,我想为这些许可证和职位之间的关系创建一个排名/优先级,请参见下面的排名,我想创建一个查询,这样,如果一个人有多个许可证,从而产生多行,我们可以通过将1优先于2优先于3优先,保留顶行,无论是1、2还是3,并删除底行

完美匹配角色-许可证:

一般-一般=1 服务-服务=1 Admin-Admmin=1 很好的匹配:

一般事务=2 服务-一般=2 不匹配:

一般-管理=3 服务-管理=3 管理-服务=3 管理-一般=3
这可能吗

我将通过创建一个未命名的子查询来解决这个问题,在该子查询中,我将明确地说明3个子查询中每个秩的值。然后,我将按照秩字段对外部查询进行排序

下面的查询将顶部的所有1分组,然后是2和3。假设您想先按个人分组,那么您应该列出可以唯一标识order by中第一个人的字段。例如:ORDER BY EmployeerNo,Rank

另外,如果您不希望rank字段出现在输出中,只需在外部查询中明确说明要选择哪些字段,而不是下面使用的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
编辑:添加请求的变更以仅报告每个员工和许可证的最高排名记录。因此,我将查询从前面连接到它本身,只是做了一些小的更改。更改是使查询成为聚合查询,其中仅报告每个许可证和员工的最大排名。我在猜测employee字段的名称。当我们内部连接这些查询时,我们将报告原始查询中与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:将最大值更改为最小值

我将通过创建一个未命名的子查询来解决此问题,在该子查询中,我将明确说明3个子查询中每个列组的值。然后,我将按照秩字段对外部查询进行排序

下面的查询将顶部的所有1分组,然后是2和3。假设您想先按个人分组,那么您应该列出可以唯一标识order by中第一个人的字段。例如:ORDER BY EmployeerNo,Rank

另外,如果您不希望rank字段出现在输出中,只需在外部查询中明确说明要选择哪些字段,而不是下面使用的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
编辑:添加请求的变更以仅报告每个员工和许可证的最高排名记录。因此,我将查询从前面连接到它本身,只是做了一些小的更改。更改是使查询成为聚合查询,其中仅报告每个许可证和员工的最大排名。我在猜测employee字段的名称。当我们内部连接这些查询时,我们将报告原始查询中与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:将最大值更改为最小值

@GordonLinoff-我看没问题。。。我缺少什么?我在Access2007中创建了一个测试访问数据库,没有问题。它可以工作。这太棒了。是否仍需要添加到此查询以删除其许可证或员工的任何较低副本?@PJS,因此,如果有一个级别2记录和一个级别3记录都具有相同的许可证和员工,您只想返回级别3记录?如果john是一名普通员工,他有两个级别1的普通许可证,除了排名2和排名3的记录,我想确保他的排名1是唯一一个显示。同样,如果他的最高排名是2,但他有一个3,我希望3被删除。@GordonLinoff-在我看来没问题。。。我缺少什么?我在Access2007中创建了一个测试访问数据库,没有问题。它可以工作。这太棒了。是否仍需要添加到此查询以删除其许可证或员工的任何较低副本?@PJS,因此,如果有一个级别2记录和一个级别3记录都具有相同的许可证和员工,您只想返回级别3记录?如果john是一名普通员工,他有两个级别1的普通许可证,除了排名2和排名3的记录,我想确保他的排名1是唯一一个显示。同样,如果他的最高排名是2,但他有一个3,我希望3被删除。