-1
I need to create a list composed by the fields Current and Next Stage both stage names coming from the same table.
Example of the result I seek:
Stage Table (stage name provider)
The query searches the Stage Table (below) the stage names in the Stages table (above):
I can even have the same ID for Current and Next.
SELECT e.Estagio AS Atual,
(SELECT e.Estagio FROM EstagiosCronologia ec
INNER JOIN Estagios e ON ec.ProximoEstagioId = e.EstagioId) AS Proximo
FROM EstagiosCronologia ec
INNER JOIN Estagios e ON ec.EstagioAtualId = e.EstagioId
This query above works for only one item - with the table Stage Ronology polluted returns me an error:
Msg 512, Level 16, State 1, Line 2 Subquery returned more than 1 value. This is not permitted when the subquery Follows =, != , <, <= , >, >= or when the subquery is used as an Expression.
How to create this query?
Table Interns
CREATE TABLE [dbo].[EstagiosCronologia] (
[CronologiaId] VARCHAR (36) NOT NULL,
[EstagioAtualId] VARCHAR (36) NOT NULL,
[ProximoEstagioId] VARCHAR (36) NOT NULL,
PRIMARY KEY CLUSTERED ([CronologiaId] ASC),
CONSTRAINT [FK_EstagiosCronologia_Estagios] FOREIGN KEY ([EstagioAtualId]) REFERENCES [dbo].[Estagios] ([EstagioId]) ON DELETE CASCADE
);
Table Stages
CREATE TABLE [dbo].[Estagios] (
[EstagioId] VARCHAR (36) NOT NULL,
[CursoId] VARCHAR (36) NOT NULL,
[Estagio] VARCHAR (50) NOT NULL,
[Duracao] INT NOT NULL,
[ValorTabela] DECIMAL (10, 2) NOT NULL,
PRIMARY KEY CLUSTERED ([EstagioId] ASC),
CONSTRAINT [FK_Estagios_Cursos] FOREIGN KEY ([CursoId]) REFERENCES [dbo].[Cursos] ([CursoId]) ON DELETE CASCADE
);
Dados Cronologia
INSERT INTO [dbo].[EstagiosCronologia] ([CronologiaId], [EstagioAtualId], [ProximoEstagioId]) VALUES (N'7027a044-9530-446f-a60d-9971a206d452', N'508ccd5b-b177-4876-9b7c-987f64ada5ef', N'bb84bd20-4907-4a92-815f-44c08b24410a')
INSERT INTO [dbo].[EstagiosCronologia] ([CronologiaId], [EstagioAtualId], [ProximoEstagioId]) VALUES (N'dc72f06c-470b-4913-b480-4106e05831b4', N'bb84bd20-4907-4a92-815f-44c08b24410a', N'c86516bc-bae2-4edf-9c75-236cdc5fccd8')
Data Interns
INSERT INTO [dbo].[Estagios] ([EstagioId], [CursoId], [Estagio], [Duracao], [ValorTabela]) VALUES (N'508ccd5b-b177-4876-9b7c-987f64ada5ef', N'dc72f06c-470b-4913-b480-4106e05831b4', N'Teste', 40, CAST(1589.00 AS Decimal(10, 2)))
INSERT INTO [dbo].[Estagios] ([EstagioId], [CursoId], [Estagio], [Duracao], [ValorTabela]) VALUES (N'bb84bd20-4907-4a92-815f-44c08b24410a', N'dc72f06c-470b-4913-b480-4106e05831b4', N'Nursery Play', 40, CAST(1950.00 AS Decimal(10, 2)))
INSERT INTO [dbo].[Estagios] ([EstagioId], [CursoId], [Estagio], [Duracao], [ValorTabela]) VALUES (N'c86516bc-bae2-4edf-9c75-236cdc5fccd8', N'dc72f06c-470b-4913-b480-4106e05831b4', N'Teste 2', 40, CAST(1500.00 AS Decimal(10, 2)))
Each internship can only have one next?
– Gabriel Oliveira
no, each stage may have more than one next.
– Nelson
The error ta happening pq the subquery returns more than one line, basically you ta tried to put 2 or more records within one, if each stage can only have one
Próximo
there is something wrong with your tableEstagiosCronologia
– Gabriel Oliveira
Can you pass me the creation scripts of the tables with some record for me to test?
– Gabriel Oliveira
Help like this - this way?
– Nelson
adds to question, if they will not delete
– Gabriel Oliveira
I answered, if that’s not what you want let me know
– Gabriel Oliveira
Sorry Gabriel Oliveira, I don’t see the answer anywhere... I’m new to Stackoverflow. How do I see your answer?
– Nelson