Вы можете создать свой набор результатов как объединение 4 категорий (каждая со своими правилами), а затем преобразовать все в json:
declare @tmp table(id int, Titulo char(1), Dominio char(1), Documento char(1), fiscalizacion char(1), Informe char(1), Investigacion char(1), Legajo char(1), Origen char(1), Categoria char(1))
insert into @tmp values
(953922 ,'N','N','N','N','N','N','N','S',3)
,(950794 ,'N','N','N','N','N','N','N','S',3)
,(957140 ,'N','N','N','N','N','N','N','S',3)
,(86068 ,'N','N','N','N','N','N','N','N',4)
,(93300 ,'N','N','N','N','N','N','N','N',4)
,(82286 ,'N','S','N','N','N','N','N','S',2)
,(92476 ,'S','N','N','N','N','N','N','N',1)
-- Categoria = 1
select id, categoria,json_query(replace(QUOTENAME(
case when Titulo ='S' then quotename('Titulo','"') + ', ' else '' end
+ case when Dominio ='S' then quotename('Dominio','"') + ', ' else '' end
+ case when Investigacion ='S' then quotename('Investigacion','"') + ', ' else '' end
),', ]',']') ) as Requisitos
from @tmp
where Categoria = 1
UNION ALL
--Categoria = 2
select id, categoria,json_query(replace(QUOTENAME(
+ case when Documento ='S' then quotename('Documento','"') + ', ' else '' end
+ case when fiscalizacion ='S' then quotename('fiscalizacion','"') + ', ' else '' end
+ case when Informe ='S' then quotename('Informe','"') + ', ' else '' end
+ case when Legajo ='S' then quotename('Legajo','"') + ', ' else '' end
),', ]',']') ) as Requisitos
from @tmp where Categoria = 2
UNION ALL
--Categoria = 3
select id, categoria,json_query(case when Origen ='S' then quotename(quotename('Origen','"') ) + ', ' else '' end) as Requisitos
from @tmp
where Categoria = 3
UNION ALL
--Categoria = 4
select id, categoria,json_query(replace(QUOTENAME(
case when Titulo ='N' then quotename('Titulo','"') + ', ' else '' end
+ case when Dominio ='N' then quotename('Dominio','"') + ', ' else '' end
+ case when Documento ='N' then quotename('Documento','"') + ', ' else '' end
+ case when fiscalizacion ='N' then quotename('fiscalizacion','"') + ', ' else '' end
+ case when Informe ='N' then quotename('Informe','"') + ', ' else '' end
+ case when Investigacion ='N' then quotename('Investigacion','"') + ', ' else '' end
+ case when Legajo ='N' then quotename('Legajo','"') + ', ' else '' end
+ case when Origen ='N' then quotename('Origen','"') + ', ' else '' end
),', ]',']') ) as Requisitos
from @tmp where Categoria = 4
for json auto , WITHOUT_ARRAY_WRAPPER
Результаты:
{
"id": 92476,
"categoria": "1",
"Requisitos": ["Titulo"]
},
{
"id": 82286,
"categoria": "2",
"Requisitos": []
},
{
"id": 953922,
"categoria": "3",
"Requisitos": ["Origen"]
},
{
"id": 950794,
"categoria": "3",
"Requisitos": ["Origen"]
},
{
"id": 957140,
"categoria": "3",
"Requisitos": ["Origen"]
},
{
"id": 86068,
"categoria": "4",
"Requisitos": ["Titulo",
"Dominio",
"Documento",
"fiscalizacion",
"Informe",
"Investigacion",
"Legajo",
"Origen"]
},
{
"id": 93300,
"categoria": "4",
"Requisitos": ["Titulo",
"Dominio",
"Documento",
"fiscalizacion",
"Informe",
"Investigacion",
"Legajo",
"Origen"]
}