Return 01 column data in 03

0

I'm working with a data in a table, in which I made a SELECT that one of the columns returns the status of the processes that are in progress in our queue. The data in this column is for example: "In progress", "Completed", "Canceled".

I need to divide these returns into 03 columns, so that:

Coluna 01 retorne: "Em andamento" 
Coluna 02 retorne: "Concluído"
Coluna 03: "Cancelado"

I'm using SQL Server.

Notice that in the Status column I have 04 different types of returns. I would like to display each return that was different, in a specific column, as if there were another 04 columns: Status 01, Status 02, Status 03, and Status 04.

Status 01 displays: Draft;
Status 02 displays: Awaiting correction,
Status 03 displays: Fill in next step;
Status 04 displays: Waiting for correction;

    
asked by anonymous 29.03.2018 / 02:53

1 answer

0

One suggestion is to use CASE:

create table tblTeste
(
  ID int,
  ID_STATUS int
)

create table tblStatus
(
    ID_STATUS int,
    STATUS varchar(50)
)

insert into tblTeste (ID, ID_STATUS) values (1, 1)
insert into tblTeste (ID, ID_STATUS) values (2, 1)
insert into tblTeste (ID, ID_STATUS) values (3, 2)
insert into tblTeste (ID, ID_STATUS) values (4, 2)
insert into tblTeste (ID, ID_STATUS) values (5, 3)
insert into tblTeste (ID, ID_STATUS) values (6, 3)
insert into tblTeste (ID, ID_STATUS) values (7, 4)
insert into tblTeste (ID, ID_STATUS) values (8, 4)

insert into tblStatus (ID_STATUS, STATUS) values (1, 'Rascunho')
insert into tblStatus (ID_STATUS, STATUS) values (2, 'Aguardando correção')
insert into tblStatus (ID_STATUS, STATUS) values (3, 'Preencher próxima etapa')
insert into tblStatus (ID_STATUS, STATUS) values (4, 'Aguardando correção')

select t.*,
    case when t.ID_STATUS = 1 then STATUS else '' end 'Status 1',
    case when t.ID_STATUS = 2 then STATUS else '' end 'Status 2',
    case when t.ID_STATUS = 3 then STATUS else '' end 'Status 3',
    case when t.ID_STATUS = 4 then STATUS else '' end 'Status 4'
from tblTeste t
inner join tblStatus s on s.ID_STATUS = t.ID_STATUS

link

    
29.03.2018 / 19:27