I need to write a request with the following requirements:
The request should return a list of all input values ββfor the client named "Steve" and for each specified date (if available) the latest status information for that date.
Customer table
CustomerID | CustomerName 1 | Steve 2 | John
Records table
CustomerID | EntryDate | EntryValue 1 | 5/4/2010 | 200.0 1 | 4/4/2010 | 100.0 1 | 3/4/2010 | 150.0 1 | 2/4/2010 | 170.0 2 | 5/4/2010 | 220.0
State table
CustomerID | StatusDate | Detail 1 | 5/28/2010 | D 1 | 4/24/2010 | S 1 | 4/5/2010 | P 1 | 2/28/2010 | A
Expected Result:
CustomerName | Date | OrderCost | Detail Steve | 5/4/2010 | 200.0 | S Steve | 4/4/2010 | 100.0 | A Steve | 3/4/2010 | 75.0 | A Steve | 3/4/2010 | 75.0 | <null>
I think the expected result may be wrong, and in fact it should be:
CustomerName | Date | OrderCost | Detail Steve | 5/4/2010 | 200.0 | S Steve | 4/4/2010 | 100.0 | A Steve | 3/4/2010 | 150.0 | A Steve | 2/4/2010 | 170.0 | <null>
Given this requirement, I do not understand why the date 3/4/2010 will occur twice, and the second time it will have a Detail. I wrote the following query:
I wrote the following query:
SELECT Customers.CustomerName, Entries.EntryDate, Entries.EntryValue, Status.Detail FROM Customers INNER JOIN Entries ON Customers.CustomerID = Entries.CustomerID LEFT OUTER JOIN Status ON Status.CustomerID = Customers.CustomersID AND Status.StatusDate <= Entries.EntryDate WHERE (Customers.CustomerName = 'Steve')
The result of my query is as follows:
CustomerName| EntryDate | EntryValue | Detail Steve | 5/4/2010 | 200.00 | S Steve | 5/4/2010 | 200.00 | P Steve | 5/4/2010 | 200.00 | A Steve | 4/4/2010 | 100.00 | A Steve | 3/4/2010 | 150.00 | A Steve | 2/4/2010 | 170.00 | NULL
Any hints of what I'm doing wrong here? I can't figure it out ...
Update I changed the order on the record, so it does not confuse us too much.