How do I sum the count of populated fields.
An example of the data.
The desired output would be.
This is what I have managed so far.
select m.project_ref,
( select count(*)
from (values (m.[EWI/IWI]), (m.Glazing), (m.Solar), (m.CWI), (m.Boiler), (m.TRV), (m.LI), (m.RIRI), (m.UFI), (m.ASHP)) as v(col)
where v.col <> ''
) as 'msr_cnt'
from SMSDB1.dbo.ops_measure m
Thank you
2
Answers
This can be done by using the conditional aggregation :
It is not entirely clear from the data provide how you are calculating msr_count, but a simple GROUP BY might be the answer here: