sql >> Base de Datos >  >> RDS >> Sqlserver

Distinguir una columna en dos tablas MS SQL Server 2008

SELECT a.CustomerId, a.CustomerName, a.UpdateDate, b.DateofBirth
FROM Table1 a
INNER JOIN (SELECT CustomerId, MAX(UpdateDate) as UDate
            FROM Table1
            GROUP BY CustomerId
            )  maxdate  ON a.CustomerId = maxdate.CustomerId
                        AND a.UpdateDate = maxdate.UDate
INNER JOIN Table2 b ON a.CustomerId = b.CustomerId

Utilice SELECCIONAR DISTINTO si tiene varios registros para un ID de cliente y una fecha actualizada.