1

I have a table that contains codes (i.e. 8202-6) and has a resulting value in a different column. I used the "Case When" function to take the codes out a single column and place them in separate columns based on the code number. However, now I end up with a bunch of null values in the rows and one code with one result. I would like to replace the actual code with it's corresponding value (i.e. 8206-6 = Height, result 75). So in the Height column I get the LOINC code instead of the Height value of 75. Can anyone help me figure out the right code to use. Below is an example of the code that I'm using just to get the columns separated.

Select Distinct

p.PatientID,
Convert(varchar, fs.obsdate, 101) as Date_of_Service,
p.LASTNAME,
p.FIRSTNAME,
CONVERT(varchar, "p"."DATEOFBIRTH", 101) as DOB,
Case When fs.loinccode='8302-2' Then fs.loinccode end as Height,
Case When fs.loinccode='3141-9' Then fs.loinccode end as Weight,
Case When fs.loinccode='8310-5' Then fs.loinccode end as Temperature,
Case When fs.loinccode='8287-5' Then fs.loinccode end as HeadCircumf,
Case When fs.loinccode='8867-4' Then fs.loinccode end as Pulse,
Case When fs.loinccode='9279-1' Then fs.loinccode end as Respiration,
Case When fs.loinccode='8480-6' Then fs.loinccode end as BPSystolic,
Case When fs.loinccode='8462-4' Then fs.loinccode end as BPDiastolic,
fs.obsvalue as Test_Results,
fs.units as Test_Units

From

PERSON as p 
Left Outer join flowsheet_observation fs on fs.pid=p.pid 

Result:

Height  Weight  Temperature HeadCircumf Pulse   Respiration BPSystolic  BPDiastolic Test_Results    Test_Units
NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    + history of lef tshoulder dislocation 10 years ago    Current Meds:        NULL
NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    never   NULL
NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    regular NULL
NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    tympanic    NULL
NULL    NULL    NULL    NULL    NULL    NULL    NULL    8462-4  100 mm Hg
NULL    NULL    NULL    NULL    NULL    NULL    8480-6  NULL    140 mm Hg
NULL    NULL    NULL    NULL    8867-4  NULL    NULL    NULL    82  /min
NULL    NULL    8310-5  NULL    NULL    NULL    NULL    NULL    99.1    deg F
NULL    3141-9  NULL    NULL    NULL    NULL    NULL    NULL    283 lb
8302-2  NULL    NULL    NULL    NULL    NULL    NULL    NULL    72.5    in
NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    37.72   NULL
NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    regular NULL
NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    tympanic    NULL
NULL    NULL    NULL    NULL    NULL    NULL    NULL    8462-4  100 mm Hg
NULL    NULL    NULL    NULL    NULL    NULL    8480-6  NULL    142 mm Hg
NULL    NULL    NULL    NULL    8867-4  NULL    NULL    NULL    86  /min
NULL    NULL    8310-5  NULL    NULL    NULL    NULL    NULL    99.4    deg F
NULL    3141-9  NULL    NULL    NULL    NULL    NULL    NULL    281 lb

Any help with this would be greatly appreciated.

4

2 回答 2

0

您需要创建一个表 code_names 将代码与其含义相关联,

code   | label
8302-2 | Height
3141-9 | Weight
...

然后加入 flowsheet_observation 并选择标签而不是代码。不需要所有这些情况。

编辑:如果创建代码表不是一个选项,则可以使用联合创建代码关系:

SELECT '8302-2' AS 代码,'Height' AS 标签 UNION SELECT '3141-9' AS 代码,'Weight' AS 标签 UNION ...

于 2013-04-13T21:04:14.993 回答
0

尝试使用 MAX(CASE WHEN THEN END) 并按其他字段分组

于 2013-04-14T01:13:18.450 回答