2016-04-30 92 views
1

樞軸表中選擇我有兩個表:如何從在MS Access

Headers Table

enter image description here

我需要得到的東西是這樣的:

HeaderID | Position1 | IsSuccessful | Position2 | IsSuccessful 
------------------------------------------------------------------------- 
    24719 |  gold_box |  true | propname25 |  false 
    24720 |  beyond |  false |  wtv |  false 
    24721 |  red_box |  false |  wtv22 |   true 

我管理了這個目前爲止:

TRANSFORM Max(Proposals.PropName) AS MaxOfPropName 
SELECT Headers.HeaderID 
FROM Headers INNER JOIN Proposals ON Headers.ProposalID = Proposals.ProposalID 
GROUP BY Headers.HeaderID 
PIVOT Headers.Position; 



HeaderID | Position1 | Position2 | 
------------------------------------------ 
    24719 |  gold_box | propname25 | 
    24720 |  beyond |  wtv | 
    24721 |  red_box |  wtv22 | 

現在我想從樞軸表中選擇,但它不工作,無法找到任何文件在網絡上,例如:

select * from (
    TRANSFORM Max(Proposals.PropName) AS MaxOfPropName 
    SELECT Headers.HeaderID 
    FROM Headers INNER JOIN Proposals ON Headers.ProposalID = Proposals.ProposalID 
    GROUP BY Headers.HeaderID 
    PIVOT Headers.Position; 
) 

是否有辦法在另一個選擇查詢(ms訪問)中使用數據透視表的結果?如何? 後來我將不得不使用一個單一的查詢像7表,並加入他們與另外3個數據透視表... :)我很抱歉這個問題的球員,但我卡住

謝謝

回答

1

考慮將帶有兩個內部聯接的派生表子查詢添加到爲每個位置保存的交叉表查詢中。如果存在更多位置,請添加更多子查詢。

交叉表查詢

TRANSFORM Max(Proposals.PropName) AS MaxOfPropName 
SELECT Headers.HeaderID 
FROM Headers 
INNER JOIN Proposals ON Headers.ProposalID = Proposals.ProposalID 
GROUP BY Headers.HeaderID 
PIVOT Headers.Position 

查詢

SELECT crosstabQ.HeaderID, crosstabQ.Position1, pos1.IsSuccessful, 
     crosstabQ.Position2, pos2.IsSuccessful 
FROM (crosstabQ  

INNER JOIN  
    (SELECT IsSuccessful 
    FROM Headers 
    WHERE Position = 1) AS pos1 
ON pos1.HeaderID = crosstabQ.HeaderID) 

INNER JOIN  
    (SELECT IsSuccessful 
    FROM Headers 
    WHERE Position = 2) AS pos2  
ON crosstabQ.HeaderID = pos2.HeaderID 

或者,運行條件​​聚集體(保存爲單獨的查詢以在下面主要查詢中使用),其是樞軸查詢的一般化版本(他重新布爾值將返回爲整數,0或-1):

SELECT Headers.HeaderID, 
     Max(IIF(Headers.Position=1, Proposals.PropName, NULL)) As Position1, 
     Max(IIF(Headers.Position=1, Headers.IsSuccessful, NULL)) As IsSuccessful1, 
     Max(IIF(Headers.Position=2, Proposals.PropName, NULL)) As Position2, 
     Max(IIF(Headers.Position=2, Headers.IsSuccessful, NULL)) As IsSuccessful2 
FROM Headers 
INNER JOIN Proposals ON Headers.ProposalID = Proposals.ProposalID 
GROUP BY Headers.HeaderID 
+0

謝謝你的人,但我的主要問題是,我正在寫Ms-Access。他不喜歡告訴他「從(一個關鍵點)選擇」 – LolaRun

+0

事實上,一個MS Access的限制。將該透視保存爲單獨保存的查詢並在main中引用它。 – Parfait

+0

是的:)這就是我昨天做的,雖然我很失望,但至少jet oledb和訪問是相當兼容的,謝謝 – LolaRun