Separate values from a multi select into columns

1

On my system, I have a multi select as they may come in the image below:

AndIdoanexportofthisvariableinthisselect

SELECTREPLACE(p.Argautor,',',';')ASArgautorFROMjud_ProcessospLEFTJOINjud_ComarcascONp.ComarcaId=c.ComarcaIdLEFTJOINjud_MunicipiosmONp.Munautor=m.MunicipioIdINNERJOINjud_EstadoseONe.EstadoId=p.EstadoId

Theresultsofthisvariableareasfollows:

I would like to know if it is possible to separate this column Argautor in several columns, as an example.

When Argautor is = 0 have a column named column0 and only show results that are 0, column1 and show only the results that are 1 and so on, I wonder if this is possible

    
asked by anonymous 26.09.2017 / 14:58

1 answer

0

Your table seems to be modeled too complicated for the desired end. I think one way out would be to have an intermediate table to store the codes for the register. Despite this you can get the result you want as follows:

SELECT CASE WHEN ',' + p.argautor + ',' LIKE '%,0,%' THEN 1 ELSE 0 END AS coluna0,
       CASE WHEN ',' + p.argautor + ',' LIKE '%,1,%' THEN 1 ELSE 0 END AS coluna1,
       CASE WHEN ',' + p.argautor + ',' LIKE '%,2,%' THEN 1 ELSE 0 END AS coluna2,
       ...
       CASE WHEN ',' + p.argautor + ',' LIKE '%,15,%' THEN 1 ELSE 0 END AS coluna15
  FROM jud_Processos p
       LEFT JOIN jud_Comarcas c ON p.ComarcaId = c.ComarcaId
       LEFT JOIN jud_Municipios m ON p.Munautor = m.MunicipioId
       INNER JOIN jud_Estados e ON e.EstadoId = p.EstadoId;

You can try to generate the same result dynamically, but the possibility of error is greater.

    
26.09.2017 / 15:11