2011-08-24 65 views
0

我有一個交叉表查詢與幾個感興趣的屬性。在這個查詢中,特定屬性的存在是或不是(1 =是0 =否)。現在,如果一個人有兩個屬性,那麼他們在數據集中由兩行表示。如果他們有三個問題,那麼他們由三行代表。我需要一種方法來使多個屬性顯示在同一行上。例如,如果他們患有糖尿病和菸草,那麼在該列下一行將具有「1」,而不是現在,在糖尿病和糖尿病全部爲0之後。而「菸草」行在每列中除了菸草(它是一個菸草)之外都有一個0。如何將交叉表查詢合併到一行中? Access 2007

table (Click to enlarge)

是的輸出看起來像一個屏幕帽。我只需要每位患者一行,而不管他們有多少問題。

下面是我的sql。

TRANSFORM IIf([tblComorbidity.comorbidityexplanation]=[tblComorbidity.comorbidityexplanation],1,0) AS Morbidity 
SELECT 
    Person.PersonID, 
    Person.Age, 
    tblKentuckyCounties.Metro, 
    tblKentuckyCounties.Appalachian, 
    Person.asiaAdmit, 
    Person.Sex 
FROM tblKentuckyCounties 
    INNER JOIN (tblComorbidity 
    INNER JOIN (Person 
     INNER JOIN tblComorbidityPerson ON Person.PersonID = tblComorbidityPerson.personID 
    ) ON tblComorbidity.ID = tblComorbidityPerson.comorbidityFK 
) ON tblKentuckyCounties.ID = Person.County 
WHERE (((tblComorbidity.comorbidityexplanation)="anxiety and depression" 
    Or (tblComorbidity.comorbidityexplanation)="heart" 
    Or (tblComorbidity.comorbidityexplanation)="respiratory" 
    Or (tblComorbidity.comorbidityexplanation)="uti" 
    Or (tblComorbidity.comorbidityexplanation)="diabetes" 
    Or (tblComorbidity.comorbidityexplanation)="hypertension" 
    Or (tblComorbidity.comorbidityexplanation)="tobacco")) 
GROUP BY 
    Person.PersonID, 
    Person.Age, 
    tblKentuckyCounties.Metro, 
    tblKentuckyCounties.Appalachian, 
    Person.asiaAdmit, 
    Person.Sex, 
    tblKentuckyCounties.Appalachian, 
    tblKentuckyCounties.Metro, 
    tblComorbidity.comorbidityexplanation 
PIVOT tblComorbidity.comorbidityexplanation; 

回答

1

如果某人有超過1個條件兩次並嘗試清理某些格式,則將該值更改爲項目的計數。在我有限的測試中它工作正常,但可能需要一些調整,因爲我沒有你的數據,但它應該讓你開始。

TRANSFORM nz(Count([tblComorbidity.comorbidityexplanation]),0) AS Morbidity 

SELECT Person.PersonID, Person.Age, tblKentuckyCounties.Metro, tblKentuckyCounties.Appalachian, Person.asiaAdmit, Person.Sex 

FROM tblKentuckyCounties INNER JOIN (tblComorbidity INNER JOIN (Person INNER JOIN tblComorbidityPerson ON Person.PersonID = tblComorbidityPerson.personID) ON tblComorbidity.ID = tblComorbidityPerson.comorbidityFK) ON tblKentuckyCounties.ID = Person.County 

WHERE tblComorbidity.comorbidityexplanation IN ('anxiety and depression', 'heart', 'respiratory', 'uti', 'diabetes', 'hypertension', 'tobacco') 

GROUP BY Person.PersonID, Person.Age, tblKentuckyCounties.Metro, tblKentuckyCounties.Appalachian, Person.asiaAdmit, Person.Sex 

PIVOT tblComorbidity.comorbidityexplanation; 
+0

我相信這是關於真棒:)。 – dzilla

相關問題