Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Distinguere una colonna in due tabelle 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

Usa SELECT DISTINCT se hai più record per un customerid e un aggiornamento.