2016-05-16 76 views
-1

我在寫一個查詢,我需要根據幾列中的內容返回一個標籤。以下是列示例。訪問查詢中的多個條件

Source Table

我需要返回是,只有在該機構列A1則未指定的標籤進行分配,其中,A1是在該機構的一列所有帳戶,和另一個機構名稱(例如a3)在其他列中是一個,那麼將分配一個部分指定的標籤。

下面是結果將會出現的列。

[所得標號] [2]

基本IIF或開關功能似乎沒有滿足給定的,有被滿足爲每個標籤多個條件的需要。

附加表信息。 Additional Table information

+0

機構1,機構2和機構3是檢查a(x)值的唯一三列嗎? – Sturgus

+0

感謝您花時間回答。最多可以有20列。我從我使用的表中添加了實際表格標題和樣本數據的另一張圖片。 – Don85203

+0

你會發現這個查詢變得非常難以創建。這完全是因爲數據模型很差。如果代理商+賬戶(+ $指定)被放置在相交表格(許多:許多)中,那麼這個問題將是微不足道的。 有多少個DX_Agency列有哪些標準?你說它上升到D20_Agency!? – Sturgus

回答

0

爲了達到預期的效果在Access中,我會嘗試取消樞軸第一個表,讓每個機構的最小值和最大值爲這樣:

SELECT A.[Account#], MIN(A.Agency) AS MinAgency, MAX(A.Agency) AS MaxAgency 
FROM 
(

    SELECT B.[Account#], B.Agency1 AS Agency 
    FROM SourceTable AS B 
    UNION 
    SELECT C.[Account#], C.Agency2 
    FROM SourceTable AS C 
    UNION 
    SELECT D.[Account#], D.Agency3 
    FROM SourceTable AS D 

) AS A 
GROUP BY A.[Account#] 

我可能這個查詢保存爲「 MinMaxAgencyQuery「,然後用它來創建我的」字母類型「列。 然後,我會在一個聯合查詢如下建立此列:

SELECT E.[Account#], "Undesignated" AS [Letter Type] 
FROM SourceTable AS E LEFT JOIN MinMaxAgencyQuery AS A ON E.[Account#] = A.[Account#] 
WHERE MinAgency = "a1" AND MaxAgency = "a1" 

UNION 

SELECT E.[Account#], "PartiallyDesignated" AS [Letter Type] 
FROM SourceTable AS E LEFT JOIN MinMaxAgencyQuery AS A ON E.[Account#] = A.[Account#] 
WHERE MinAgency = "a1" AND MaxAgency <> "a1" 

UNION 

SELECT E.[Account#], "Designated" AS [Letter Type] 
FROM SourceTable AS E LEFT JOIN MinMaxAgencyQuery AS A ON E.[Account#] = A.[Account#] 
WHERE MinAgency > "a1" 

當然,這種解決方案依賴於「A1」,實際上是「A1」(因此,低於其他機構在做字符串時-comparison)。

如果我需要做的這一切都在同一個查詢,我會代替我的第一個查詢在三個聯合在一起的查詢,像這樣:

SELECT E.[Account#], "Undesignated" AS [Letter Type] 
FROM SourceTable AS E LEFT JOIN (
SELECT A.[Account#], MIN(A.Agency) AS MinAgency, MAX(A.Agency) AS MaxAgency 
    FROM 
    (

     SELECT B.[Account#], B.Agency1 AS Agency 
     FROM SourceTable AS B 
     UNION 
     SELECT C.[Account#], C.Agency2 
     FROM SourceTable AS C 
     UNION 
     SELECT D.[Account#], D.Agency3 
     FROM SourceTable AS D 

    ) AS A 
GROUP BY A.[Account#] 

) AS F ON E.[Account#] = F.[Account#] 
WHERE MinAgency = "a1" AND MaxAgency = "a1" 

UNION 

SELECT E.[Account#], "PartiallyDesignated" AS [Letter Type] 
FROM SourceTable AS E LEFT JOIN (
SELECT A.[Account#], MIN(A.Agency) AS MinAgency, MAX(A.Agency) AS MaxAgency 
    FROM 
    (

     SELECT B.[Account#], B.Agency1 AS Agency 
     FROM SourceTable AS B 
     UNION 
     SELECT C.[Account#], C.Agency2 
     FROM SourceTable AS C 
     UNION 
     SELECT D.[Account#], D.Agency3 
     FROM SourceTable AS D 

    ) AS A 
GROUP BY A.[Account#] 

) AS F ON E.[Account#] = F.[Account#] 
WHERE MinAgency = "a1" AND MaxAgency <> "a1" 

UNION 

SELECT E.[Account#], "Designated" AS [Letter Type] 
FROM SourceTable AS E LEFT JOIN (
SELECT A.[Account#], MIN(A.Agency) AS MinAgency, MAX(A.Agency) AS MaxAgency 
    FROM 
    (

     SELECT B.[Account#], B.Agency1 AS Agency 
     FROM SourceTable AS B 
     UNION 
     SELECT C.[Account#], C.Agency2 
     FROM SourceTable AS C 
     UNION 
     SELECT D.[Account#], D.Agency3 
     FROM SourceTable AS D 

    ) AS A 
GROUP BY A.[Account#] 

) AS F ON E.[Account#] = F.[Account#] 
WHERE MinAgency > "a1" 

你永遠不指定帳號的什麼變成對於Agency1,Agency2和Agency3沒有任何價值,所以在此解決方案下它們不會顯示。

+0

如果只有MS Access支持Common Table Expressions,最後的查詢會更短(並且執行速度更快)... – Sturgus