Return data from 01 column in 03

Asked

Viewed 95 times

0

I am working with some 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 are 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 am using SQL Server.

inserir a descrição da imagem aqui

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

Status 01 displays: Draft;
Status 02 displays: Awaiting correction;
Status 03 displays: Fill next step;
Status 04 displays: Awaiting correction;

  • It was not clear to me the return you want, could add in question 1 example of how you want the data.

  • You want the lines of each status to return the processes that have that status Or it would be a computer How many processes have each status?

  • Show the table structure with some sample data. If possible, create a fiddle here: http:/sqlfiddle.com/

1 answer

0


A 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

http://sqlfiddle.com/#! 18/d36ac/1

Browser other questions tagged

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