WITH RECURSIVE desks_sub_import_open as (select desk_id,desk_id as parent_desk,import_open from desks where user_id = {user_id} and parent_desk = {parent_desk} union all select desks.desk_id,desks_sub_import_open.parent_desk,desks.import_open from (select * from desks where user_id = {user_id}) desks INNER JOIN desks_sub_import_open on desks_sub_import_open.desk_id = desks.parent_desk) select desks.desk_id, desk_code,alwo_browsing_import from (select * from desks where user_id = {user_id}) desks join (select parent_desk d, (case when sum(import_open) then 1 else 0 end) alwo_browsing_import from desks_sub_import_open group by parent_desk) alwo_browsing_import on desks.desk_id = alwo_browsing_import.d 


