Hi,
First, you may use the select you already have as an inline
view for another select, like this:
select
numberid,
cast(case when c1 >= 1 then 1 else 0 end as int)+
cast(case when c2 >= 1 then 1 else 0 end as int)+
...
cast(case when c0 >= 1 then 1 else 0 end as int) as jbcount
from
(
select
numberid,
cast(case when jb1 = 1 then '1' else '0' end as int)+
cast(case when jb2 = 1 then '1' else '0' end as int)+
cast(case when jb3 = 1 then '1' else '0' end as int)+
cast(case when jb4 = 1 then '1' else '0' end as int)+
cast(case when jb5 = 1 then '1' else '0' end as int)+
cast(case when jb6 = 1 then '1' else '0' end as int) as c1,
cast(case when jb1 = 2 then '1' else '0' end as int)+
cast(case when jb2 = 2 then '1' else '0' end as int)+
cast(case when jb3 = 2 then '1' else '0' end as int)+
cast(case when jb4 = 2 then '1' else '0' end as int)+
cast(case when jb5 = 2 then '1' else '0' end as int)+
cast(case when jb6 = 2 then '1' else '0' end as int) as c2,
.......
as c0
from my_table
)
Of course, if your database supports such syntax.
What in fact you do here, is to count the distinct values
of your six columns jb1,jb2,jb3,jb4,jb5,jb6,
so you can do this also as follows, supposing that each
value is always one of 1,2,3,4,5,6,7,8,9,0.
select numberid,
1 +
cast(case when jb2 = jb1
then 0 else 1 end as int) +
cast(case when (jb3 = jb1 or jb3 = jb2)
then 0 else 1 end as int) +
cast(case when (jb4 = jb1 or jb4 = jb2 or jb4 = jb3)
then 0 else 1 end as int) +
...
a.s.o. up to jb6 as jbcount
--
from my_table
Hope this could help.
Rgds,
Iudith