2016-12-12 79 views
1

我正在嘗試爲我的組織創建一個關於我們的員工以及他們的工作相關許可證的數據庫。有工作和牌照100S,我已經分時段分爲三個桶,試圖將它們對齊:1.一般員工,2局3.服務MS Access:創建排名/優先級並顯示最高行

但是我們有幾個問題:

  1. ,因爲許可證也有代言,所以被認爲是服務人員但具有「普通員工」許可證的人員仍然可以在服務中工作(因爲我們假設他們有代言服務)

  2. 我們如果他們的許可證和職位可能不匹配,則不想刪除用戶。

  3. 教育工作者可以在同一組/桶的屁股以及多個許可證

所以我想創建一個許可證和位置(見下面的排名)之間的這些關係的排序/優先級我想要創建一個查詢,以便如果一個人擁有多個許可證(從而生成多條生產線),我們可以保持最高水平,無論是1,2或3(通過優先於1s超過2s超過3s),以及刪除底線。

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

  • 一般 - 一般= 1
  • 服務 - 服務= 1個
  • 管理 - Admmin = 1

般配:

  • General - Services = 2
  • 服務 - 一般= 2

壞匹配:

  • 一般 - 管理= 3
  • 服務 - 管理= 3個
  • 管理 - 服務= 3
  • 管理 - 通用= 3

這可能嗎?

回答

0

我會通過創建一個未定貨的子查詢來解決這個問題,其中我明確指出每個3個子查詢中每個等級的值。然後,我通過rank字段排序外部查詢。

下面的查詢將頂部的所有1,然後是2和3進行分組。假設你想先按人進行分組,你可以列出任何可以唯一標識該人的第一個字段。例如:「ORDER BY EmployerNo,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 

編輯:添加請求的變化以僅報告每個員工和許可證的最高排名記錄。所以,我從早些時候就把自己的查詢加入自己,並做了一些小改動。更改是使查詢成爲彙總查詢,其中只報告每個許可證和員工的最高排名。 (我猜的是員工領域的名字)。當我們內部連接這些查詢時,我們會報告原始查詢中與MaxRank,許可證#和員工#匹配的所有字段。

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:改變最大到最小

+0

這查詢不MS訪問工作。 –

+0

@GordonLinoff - 看起來對我來說......我錯過了什麼? – jleach

+0

@ jdl134679。 。 。子查詢中的'union'。 –