Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most recent record in a left join

Imagine I have the following 3 tables in SqlServer:

Customer (CustomerID, FirstName, LastName) Address (AddressID, CustomerID, Line1, City, State) Product (ProductID, CustomerID, Description) 

A customer can have multiple delivery addresses and mulitple products.

What I would like to do is to list the number of customers for each State where the State is determined by the most recent Address record. Such as "How many customers last received a product in each State?". Therefore I'm not interested in any previous Address records for the Customer, only the most Recent (determined by AddressID).

State | Number of Customers -------------------------- CA    | 32 GA    | 12 TX    | 0 OH    | 18 

I would normally do something like:

SELECT a.State, count(c.CustomerID) FROM Product p INNER JOIN Customer c ON c.CustomerID = p.CustomerID LEFT JOIN Address a ON a.CustomerID = c.CustomerID WHERE p.ProductID = 101 GROUP BY a.State 

However, as a Customer may have multiple Addresses will the customer only be counted in the State of the most recent Address record?

P.S. The above is purely an example scenario to easily explain the joins I am trying to achieve and does not reflect an actual system design.

like image 692
David Glenn Avatar asked Apr 07 '09 11:04

David Glenn


People also ask

How do I find the latest record in SQL?

Here is the syntax that we can use to get the latest date records in SQL Server. Select column_name, .. From table_name Order By date_column Desc; Now, let's use the given syntax to select the last 10 records from our sample table.

How do I select the last one in SQL?

To select the last row, we can use ORDER BY clause with desc (descending) property and Limit 1. Let us first create a table and insert some records with the help of insert command.

What is left outer join in SQL with example?

A left outer join is a method of combining tables. The result includes unmatched rows from only the table that is specified before the LEFT OUTER JOIN clause. If you are joining two tables and want the result set to include unmatched rows from only one table, use a LEFT OUTER JOIN clause or a RIGHT OUTER JOIN clause.

What is left join and inner join?

Different Types of SQL JOINs Here are the different types of the JOINs in SQL: (INNER) JOIN : Returns records that have matching values in both tables. LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table.


2 Answers

Try this:

SELECT a.State, count(c.CustomerID) FROM Product p INNER JOIN Customer c ON c.CustomerID = p.CustomerID LEFT JOIN Address a ON a.CustomerID = c.CustomerID        AND a.AddressID =          (            SELECT MAX(AddressID)             FROM Address z             WHERE z.CustomerID = a.CustomerID         ) WHERE p.ProductID = 101 GROUP BY a.State 
like image 69
cjk Avatar answered Sep 20 '22 11:09

cjk


You could also try (assuming I remember my SQLServer syntax correctly):

SELECT state, count(customer_id) FROM (     SELECT         p.customer_id         , (SELECT TOP 1 State FROM Address WHERE Address.CustomerID = p.CustomerID ORDER BY Address.ID DESC) state     FROM Product p     WHERE p.ProductID = 101) GROUP BY state 
like image 39
Hank Gay Avatar answered Sep 18 '22 11:09

Hank Gay