MS Access:创建排名/优先级&显示最高行

时间:2016-12-12 17:48:41

标签: sql ms-access ms-access-2013

我正在尝试为我的组织创建一个关于我们的员工以及他们为工作所持有的相关许可证的数据库。我已经将100多个工作和许可证分配到三个桶中以尝试对齐它们:1。一般员工,2。管理,3。服务

但是我们有一些问题:

  1. 因为许可证也有认可,所以有人被认为是服务人员但是有一般雇员"许可证仍然可以在服务中工作(因为我们认为他们认可他们在服务中工作)

  2. 如果用户的许可和职位可能不匹配,我们不想删除用户。

  3. 教育工作者可以在同一组/桶中拥有多个许可证

  4. 因此,我想为许可证和职位之间的这些关系创建排名/优先级(请参阅下面的排名),我想创建一个查询,以便一个人拥有多个许可证(从而生成多个行) ,我们可以保持顶线是否是1,2或3(通过优先选择1s超过2s超过3s),并删除底线。

    完美匹配(角色 - 许可):

    • 一般 - 一般= 1
    • 服务 - 服务= 1
    • 管理员 - Admmin = 1

    好比赛:

    • 一般 - 服务= 2
    • 服务 - 一般= 2

    糟糕的比赛:

    • 一般 - Admin = 3
    • 服务 - Admin = 3
    • 管理员 - 服务= 3
    • 管理员 - 一般= 3

    这可能吗?

1 个答案:

答案 0 :(得分:0)

我会通过创建一个uninoned子查询来解决这个问题,其中我明确说明了每个3个子查询中每个等级的值。然后,我按排名字段订购外部查询。

以下查询将顶部的所有1分组,然后分为2分和3分。假设您希望首先按人分组,您可以列出任何可以在订单中首先唯一标识该人的字段。例如:“ORDER BY EmployerNo,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

编辑:添加请求的变体仅报告每位员工和许可证的排名最高的记录。所以,我正在加入从早期到自身的查询,只做了一些小改动。更改是使查询成为聚合查询,其中仅报告每个许可证和员工的最大等级。 (我猜的是雇员字段的名称)。当我们内部加入这些查询时,我们将报告原始查询中与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:将最大值更改为分钟