0
I created a query that concatenates my results. It is a table with names and to which range (square) they are classified. The table is called consulta_readiness and the fields are name and squared:
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]
Since I only have the option of 9 possible squares, the query comes like this, for example:
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 it a little differently: that there were nine result columns called Square 1, Square 2, Square 3, ... , Square 9. The SELECT query would only come with a row like this, for example:
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 any idea how I can do it? It’s also kind of a concatenation, I think. But I haven’t figured out the best way yet.
More clearly, you want to turn rows into columns? I think you have something about this here in the OS. It’s used in Mysql?
– Inkeliz
This is called pivot, you can take a look at this link https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=0ahUKEwjx16TG49jRAhXCF5AKHT_RCQFggaMA&url=http%3A%2F%2Fwww.devmedia.com.br%2Fpivot-no-sql-server-inverted-lines-e-columns-in-a-one-.real-example%2F28318&usg=Afqjcnhgeufsokw_tncpiivuk5do79nqsq&bvm=bv.144686652,d.Y2I&Cad=rja
– Jeferson Almeida