我有一个名为Identifier的表,它有identifierType、identifierValue和patient表的外键。
一个患者可以有多个标识符,因此对于给定的患者,标识符表中将有多行。
我想从这个表中提取满足给定条件的患者外键的值,
一个例子是我想要找到
patientId where identifierType = 'PatientFirst"
and identifierValue = 'sally'
and identifierType= 'patientFirst'
and identifier value = 'sally'.
在sqlserver中提取此结果的sql语句是什么
参考资料:( http://sqlfiddle.com/#!3/33fc6/2/0 )
发布于 2013-10-12 23:38:29
这个网站看起来有点简单,不是吗?:)
SELECT fk_patientID
FROM identifier
WHERE IdentifierType = 'PatientFirst'
AND IdentifierValue = 'sally'
发布于 2013-10-13 02:14:16
如果您希望将某些属性展平到每个患者ID的一行中,则数据透视表也可能对您很有用:
;with PatientFullName( fk_patientId, PatientLast, PatientFirst )
as
(
select
fk_patientId
, pt.PatientLast
, pt.PatientFirst
from
Identifier
pivot
(
MAX(identifierValue)
for identifierType in ( [PatientLast], [PatientFirst] )
) as pt
)
select
*
from
PatientFullName pfn
where
pfn.PatientLast = 'Doe'
and pfn.PatientFirst = 'Sally'
https://stackoverflow.com/questions/19340249
复制相似问题