2011-08-24 172 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