Return 1 pair by SQL Id SERVER

1

I have the following query

SELECT IdentificadorAutomovil, Automovil.Titulo, Automovil.Precio, Automovil.Anio, Automovil.Kilimetros, Automovil.Ubicacion, Imagenes.Imagenes 

FROM Automovil INNER JOIN Imagenes

ON Automovil.IdentificadorAutomovil = Imagenes.Automovil

But I'd like you to only return 1 pair for Automobile Identifier, and I can not achieve this,

    
asked by Bruno Sosa Fast Tag 21.09.2017 в 16:22
source

2 answers

0

You could use APPLY (efficiently) to get the first of each row.

For example:

SELECT 
    A.IdentificadorAutomovil
   ,A.Titulo
   ,R.Imagenes
FROM Automovil A
    CROSS APPLY
( 
    SELECT TOP 1 I.Imagenes
    FROM Imagenes I
    WHERE I.Automovil = A.IdentificadorAutomovil
) R

DEMO

    
answered by 21.09.2017 / 17:01
source
1

In case you want to group by car you would be using the GROUP BY operator as follows:

SELECT
    IdentificadorAutomovil ,
    Automovil.Titulo ,
    Automovil.Precio ,
    Automovil.Anio ,
    Automovil.Kilimetros ,
    Automovil.Ubicacion ,
    Imagenes.Imagenes
FROM
    Automovil
    INNER JOIN Imagenes ON Automovil.IdentificadorAutomovil = Imagenes.Automovil

GROUP BY 
    IdentificadorAutomovil,
    Automovil.Titulo ,
    Automovil.Precio ,
    Automovil.Anio ,
    Automovil.Kilimetros ,
    Automovil.Ubicacion ,
    Imagenes.Imagenes

The issue is that if the column of images differs from one another you will keep bringing 2 records, since it shows you one image per record.

    
answered by 21.09.2017 в 16:39