Separate values from a multi select in columns

Asked

Viewed 532 times

1

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

inserir a descrição da imagem aqui

And I export this variable in this select

SELECT 
REPLACE(p.Argautor, ',', ';') AS Argautor
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

the results of this variable come like this:

inserir a descrição da imagem aqui

I wonder if it is possible to separate this column Mortar in several columns, as an example.

when Mortar for = 0 have a column called columnar and shows only the results that are 0, column1 and to show only the results that are 1 so successively, I would like to know if this is possible

  • So you just want one row and several columns?

  • yes only that as you can see in the photo in a row has several values, example, the column0 can only appear values that are 0 can not show other values

1 answer

0


Your table seems to be modeled in a way too complicated for the desired purpose. I believe that a way out would be to have an intermediate table to store the registration codes. Despite this you can reach the result you want in the following way:

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.

  • 1

    Man that’s just what I wanted, that’s right, thank you so much for your help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.