Skip to content
Advertisement

How to properly perform a SELECT query [closed]

I have this query in SQL Server:

SELECT
    (SELECT moduloT.diagnostico FROM BdMINIPlus.dbo.moduloT AS moduloT WHERE moduloT.idPaciente=612) AS moduloT,
    (SELECT CASE WHEN moduloU.preguntaU8 = 'Si' OR moduloU.preguntaU9 = 'Si' THEN 'Si' ELSE 'No' END FROM BdMINIPlus.dbo.moduloU AS moduloU WHERE moduloU.idPaciente=612) AS moduloU,
    (SELECT moduloW.diagnostico FROM BdMINIPlus.dbo.moduloW AS moduloW WHERE moduloW.idPaciente=612) AS moduloW,
    (SELECT moduloX.diagnostico FROM BdMINIPlus.dbo.moduloX AS moduloX WHERE moduloX.idPaciente=612) AS moduloX,
    (SELECT moduloY.diagnostico FROM BdMINIPlus.dbo.moduloY AS moduloY WHERE moduloY.idPaciente=612) AS moduloY;

I get this result:

The subquery returned more than one value, which is not correct when followed by =,! =, <, <=,>,> = Or when used as an expression.

I do not understand why it is due and how to correct it, I would greatly appreciate it if you could give me an idea of ​​how to solve it.

Advertisement

Answer

Just another option is to re-write the queries as a JOIN … Perhaps multiple records should be expected.

Example

Select moduloT = T.diagnostico
      ,moduloU = CASE WHEN U.preguntaU8 = 'Si' OR U.preguntaU9 = 'Si' THEN 'Si' ELSE 'No' END
      ,moduloW = W.diagnostico
      ,moduloX = X.diagnostico
      ,moduloY = Y.diagnostico
 From  BdMINIPlus.dbo.moduloT T
 Left Join  BdMINIPlus.dbo.moduloU U on T.idPaciente = U.idPaciente
 Left Join  BdMINIPlus.dbo.moduloW W on T.idPaciente = W.idPaciente
 Left Join  BdMINIPlus.dbo.moduloX X on T.idPaciente = X.idPaciente
 Left Join  BdMINIPlus.dbo.moduloY Y on T.idPaciente = Y.idPaciente
 Where T.idPaciente=612
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement