1

我有一个存储在表中的值,其中包含多项选择答案的结果。选项看起来与此类似:

Allergy 1
Cardiology  2
Chest Disease   4
Dermatology 8
Dialysis    16
Emergency Room  32
Ambulance Trips 64
Gastroenterology    128
General Medicine    256
General Surgery 512
Gynecology  1024
Hematology  2048
Methadone   4096
Neurology   8192
Neurosurgery    16384
Obstetrics  32768
Ophthalmology   65536
Orthopedics 131072
Physical Therapy    262144
Plastic Surgery 524288
Podiatry    1048576
Proctology  2097152
Psychiatry  4194304
Surgery Performed   8388608
Thoracic Surgery    16777216
Urology 33554432
Outside X-Rays  67108864
Diagnostic Test(outside)    134217728
Other Trips/Outside Consults    268435456

当有人填写答案时,答案/值将存储如下:

id                   form_record form_item   value                entered_date
-------------------- ----------- ----------- -------------------- -----------------------
376418               17453       2943        1114241              2013-05-23 09:56:06.000
376889               17475       2943        1056896              2013-05-23 11:00:06.000

因此,当我尝试查看用户选择的内容时,例如,Neurology今天我做了这样的事情:

select
    fa.value & 8192 as 'Neurology'
from
    form_records fr
        inner join form_names fn on fn.id = fr.form_Name
        inner join form_items fi on fn.id = fi.form_name
        inner join form_answers fa on fr.id = fa.form_record
            and fa.form_item = fi.id
where
    fn.name like '%Consultation Review Form%'   
    and dateadd(dd, 0, datediff(dd, 0, fr.date)) = dateadd(dd, 0, datediff(dd, 0, getdate()))

我的结果是这样的:

Neurology
--------------------
0
8192

(2 row(s) affected)

我知道完成此答案Neurology的第二个用户已选择,但完成此答案的第一个用户未选择Neurology

我想要做的是计算Neurology答案/值的数量。

通过做这个:

select
    count(fa.value & 8192) as 'Neurology'

退货2。我希望它返回1

4

1 回答 1

3
select
    count(NULLIF(fa.value & 1, 0)) as 'Neurology'
    ...

这会将“不匹配”更改为 NULL,并且 COUNT 将忽略 NULL。

于 2013-05-23T15:24:42.117 回答