Ваша проблема в том, что ваша производная таблица не содержит столбец Room
. Вы можете отфильтровать по Room
в производной таблице:
select Disease, count(*) total
from (
select Disease from tb_data where Room = 'Man'
union all select Additional_Disease1 from tb_data where Room = 'Man'
union all select Additional_Disease2 from tb_data where Room = 'Man'
union all select Additional_Disease3 from tb_data where Room = 'Man'
union all select Additional_Disease4 from tb_data where Room = 'Man'
) t
where Disease is not Null
group by Disease
order by total desc, Disease
или включить столбец Room
в производной таблице:
select Disease, count(*) total
from (
select Room, Disease from tb_data
union all select Room, Additional_Disease1 from tb_data
union all select Room, Additional_Disease2 from tb_data
union all select Room, Additional_Disease3 from tb_data
union all select Room, Additional_Disease4 from tb_data
) t
where Disease is not Null
and Room = 'Man'
group by Disease
order by total desc, Disease