SELECT语句的额外行,交叉表?访问2007

时间:2011-08-24 12:25:41

标签: sql tsql ms-access

我正在与一些生物抑制器人合作,当然他们喜欢SAS。我在下面有一个选择声明,用于测试一个人可能存在的某些问题。这是一个二元的东西,所以他们要么做,要么不做。如果一个人有心脏问题和呼吸问题,那么他们的患者ID将被列出两次。如何为每个发病率添加额外的1或0列?因此,如果我有三个问题,并且它们是“HEART”,“LUNG”和“UTI”,则会生成一个额外的列,根据一个人是否存在该问题而具有1或0。

我想我可以使用Excel将其作为交叉表,但最终它需要采用该格式。下面是我的SELECT语句。谢谢,伙计们!

编辑:

TRANSFORM First(Person.PersonID) AS Morbidity
SELECT Person.PersonID, Person.Age, Person.Sex
FROM tblKentuckyCounties INNER JOIN ((tblComorbidity INNER JOIN comorbidVisits ON tblComorbidity.ID = comorbidVisits.comorbidFK) 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)="hypertension" Or (tblComorbidity.comorbidityexplanation)="pressure sores" Or (tblComorbidity.comorbidityexplanation)="tobacco" Or (tblComorbidity.comorbidityexplanation)="uti"))
GROUP BY Person.PersonID, Person.Age, Person.Sex, tblComorbidity.comorbidityexplanation
PIVOT Person.Race;

1 个答案:

答案 0 :(得分:2)

未经测试:

TRANSFORM IIf([c.comorbidityexplanation]=
      [c.comorbidityexplanation],1,0) AS Morbidity
SELECT p.PersonID, p.Age, p.Sex, p.Race
FROM tblKentuckyCounties kc
INNER JOIN ((tblComorbidity c
INNER JOIN comorbidVisits cv
   ON c.ID = cv.comorbidFK) 
INNER JOIN (Person p
INNER JOIN tblComorbidityPerson cp
   ON p.PersonID = cp.personID) 
   ON c.ID = cp.comorbidityFK) 
   ON kc.ID = p.County
GROUP BY p.PersonID, p.Age, p.Sex, p.Race
PIVOT c.comorbidityexplanation