SQL - Select with possible concatenation

0

I created a query that concatenates my results. It is a table with names and which range (square) they are sorted. The table is named prompt_name and the fields are name and square :

Select Main.Quadrado,
   Left(Main.Nome,Len(Main.Nome)-1) As "Nome"
From
(
    Select distinct ST2.Quadrado, 
        (
            Select ST1.Nome + ','
            From dbo.Consulta_Prontidao ST1
            Where ST1.Quadrado = ST2.Quadrado
            AND ST1.codigoUnidade = 45
            AND ST1.codigoPeriodo = 5
            ORDER BY ST1.Quadrado
            For XML PATH ('')
        ) [Nome]
    From dbo.Consulta_Prontidao ST2
) [Main]

Given that I have only the option of 9 possible squares, the query comes like this:

Quadrado | Nomes
-------- | -----
1        | NULL
2        | Maria,Tamiris
3        | Kellen
4        | Elis
5        | Paulo,Alex
6        | Mauro,Rodrigo,Elaine,Gabriela
7        | NULL
8        | Teixeira,Luis
9        | NULL

But I would like to do a bit differently: that there were nine result columns called Square 1, Square 2, Square 3, ..., Square 9. The SELECT query would only come with a line like this:

Quadrado 1 | Quadrado 2    | Quadrado 3 | Quadrado 4 | Quadrado 5 | Quadrado 6                    | Quadrado 7 | Quadrado 8     | Quadrado 9
---------- | ------------- | ---------- | ---------- | ---------- | ----------------------------- | ---------- | -------------- | ----------
NULL       | Maria,Tamiris | Kellen     | Elis       | Paulo,Alex | Mauro,Rodrigo,Elaine,Gabriela | NULL       | Teixeira, Luis | NULL

Does anyone have an idea how I can do it? It's also sort of a concatenation, I think. But I have not figured out the best way yet.

    
asked by anonymous 23.01.2017 / 18:12

1 answer

0

I think you want to turn rows into columns. There are already several topics that address the same subject. See one here .

I made an example using oracle, which you can use as the basis for your database:

create table consulta_prontidao(nome VARCHAR(30), quadrado VARCHAR(30));


insert into consulta_prontidao values ('1', NULL);

insert into consulta_prontidao values ('2','Maria');
insert into consulta_prontidao values ('2','Tamiris');
insert into consulta_prontidao values ('3','Kellen');
insert into consulta_prontidao values ('4','Elis');
insert into consulta_prontidao values ('5','Paulo');
insert into consulta_prontidao values ('5','Alex');
insert into consulta_prontidao values ('6','Mauro');
insert into consulta_prontidao values ('6','Rodrigo');
insert into consulta_prontidao values ('6','Elaine');
insert into consulta_prontidao values ('6','Gabriela');
insert into consulta_prontidao values ('7', NULL);
insert into consulta_prontidao values ('8', 'Teixeira');
insert into consulta_prontidao values ('8', 'Luis');
insert into consulta_prontidao values ('9', NULL);

After the assembled table, just do the string concatenation (in this case, LISTAGG ) along with pivot

select * from consulta_prontidao
pivot (
       LISTAGG(quadrado, ',') WITHIN GROUP (ORDER BY quadrado)
       for nome in ('1'  as QUADRADO_1,
                    '2'  as QUADRADO_2,
                    '3'  as QUADRADO_3,
                    '4'  as QUADRADO_4,
                    '5'  as QUADRADO_5,
                    '6'  as QUADRADO_6,
                    '7'  as QUADRADO_7,
                    '8'  as QUADRADO_8,
                    '9'  as QUADRADO_9
      ));

Result

EDIT:

ApparentlyyouworkwithMySql,soIsuggestusing GROUP_CONCAT ()

Looking like this:

select * from consulta_prontidao
pivot (
       group_concat(quadrado ',')
       for nome in ('1'  as QUADRADO_1,
                    '2'  as QUADRADO_2,
                    '3'  as QUADRADO_3,
                    '4'  as QUADRADO_4,
                    '5'  as QUADRADO_5,
                    '6'  as QUADRADO_6,
                    '7'  as QUADRADO_7,
                    '8'  as QUADRADO_8,
                    '9'  as QUADRADO_9
      ));

I can not test the MySql implementation at this time. Test and let me know if there is a problem that I can fix.

In SqlServer 2016 use STRING_AGG :

select * from consulta_prontidao
pivot (
       STRING_AGG (quadrado, ',')
       for nome in ('1'  as QUADRADO_1,
                    '2'  as QUADRADO_2,
                    '3'  as QUADRADO_3,
                    '4'  as QUADRADO_4,
                    '5'  as QUADRADO_5,
                    '6'  as QUADRADO_6,
                    '7'  as QUADRADO_7,
                    '8'  as QUADRADO_8,
                    '9'  as QUADRADO_9
      ));
    
23.01.2017 / 18:29