我有一个存储在表格中的值,其中包含多项选择答案的结果。选择看起来类似于:
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)
我知道完成此答案的seconduser选择了Neurology
,但是第一个完成此答案的用户没有选择Neurology
。
我要做的是获取Neurology
个答案/值的当天的计数。
通过这样做:
select
count(fa.value & 8192) as 'Neurology'
返回2
。我希望它返回1
。
答案 0 :(得分:3)
select
count(NULLIF(fa.value & 1, 0)) as 'Neurology'
...
这会将“不匹配”更改为NULL,而COUNT将忽略NULL。