Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 要选择语句的额外行,交叉表?访问2007_Sql_Tsql_Ms Access - Fatal编程技术网

Sql 要选择语句的额外行,交叉表?访问2007

Sql 要选择语句的额外行,交叉表?访问2007,sql,tsql,ms-access,Sql,Tsql,Ms Access,我和一些biostats的人一起工作,他们当然喜欢SAS。我在下面有一个select语句,用于测试一个人可能存在的某些问题。这是一个二进制的东西,所以他们要么做,要么不做。如果一个人有心脏问题和呼吸系统问题,那么他们的患者将被列入两次名单。如何为每种发病率添加1或0的额外列?因此,如果我有三个问题,它们是“心脏”、“肺”和“UTI”,则会生成一个额外的列,根据是否存在该问题,该列的值为1或0 我想我可以使用Excel使其成为交叉表,但最终它将需要采用这种格式。下面是我的选择声明。谢谢大家 编辑:

我和一些biostats的人一起工作,他们当然喜欢SAS。我在下面有一个select语句,用于测试一个人可能存在的某些问题。这是一个二进制的东西,所以他们要么做,要么不做。如果一个人有心脏问题和呼吸系统问题,那么他们的患者将被列入两次名单。如何为每种发病率添加1或0的额外列?因此,如果我有三个问题,它们是“心脏”、“肺”和“UTI”,则会生成一个额外的列,根据是否存在该问题,该列的值为1或0

我想我可以使用Excel使其成为交叉表,但最终它将需要采用这种格式。下面是我的选择声明。谢谢大家

编辑:

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;
这没有经过测试:

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

显然,这个结果集太大了,不能作为交叉表。希望我明确表示,我需要一个0或1的标准'烟草,尿路感染,压疮的存在为基础。对每个人来说都是如此。因此,如果他们只有心脏问题,他们会得到1分,其余的0分。我想我可以在Excel中处理这个问题,但我更喜欢SQL方法。@dzilla我已经编辑过,可以得到1和0。至于大小,也许您可以从较少的表中创建交叉表,然后基于交叉表和额外的表创建第二个查询,以获取其他所需的数据,例如年龄和性别。我不确定这是否适用于较小的结果集。我试图使用TOP方法仅选择一个小子集,但它不允许我在交叉表查询中这样做。@dzilla您应该能够通过基于另一个by WHERE查询来限制集合。在完整的数据集上运行什么?好的,我现在有一个完整的数据集,在交叉表格式中有正确的行数,但它看起来不正确。我编辑了我现在拥有的SQL语句。