Home All Groups Group Topic Archive Search About

SQL Query - Isolating last row in customer record

Author
3 Feb 2006 6:20 AM
Malcolm via SQLMonster.com
Hi,,
I have a simple MS Access database that contains two tables at present.
Customer_Info and Payment_Details.

I want to run a query that will give me a list of customers who owe money on
their account based on the date in the SQL query.

The problem is that if I put in a date I  am getting all the records for each
customer before the date specified where I am only looking for the last
record which lists their last payment details.

Can anyone help me??  Malcolm


Author
3 Feb 2006 12:30 PM
Tony Scott
Malcolm,

From the sounds of it you simply want to return all Customer records from
the Customer_Info table and the last record from the Payment_Details for the
customers. If this is the case you can use something similar to this::

SELECT Cus_ID, MAX(Pay_Date) as PayDate
FROM Customer_Info cus LEFT JOIN Payment_Details pay
ON cus.Cus_ID pay.Cus_ID
GROUP BY Cus_ID

Hope this assists,

Tony

Show quote
"Malcolm via SQLMonster.com" wrote:

> Hi,,
> I have a simple MS Access database that contains two tables at present.
> Customer_Info and Payment_Details.
>
> I want to run a query that will give me a list of customers who owe money on
> their account based on the date in the SQL query.
>
> The problem is that if I put in a date I  am getting all the records for each
> customer before the date specified where I am only looking for the last
> record which lists their last payment details.
>
> Can anyone help me??  Malcolm
>
> --
> Message posted via SQLMonster.com
> http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server-programming/200602/1
>
Author
7 Feb 2006 1:28 AM
Malcolm via SQLMonster.com
Thanks a bunch Tony, that helped. Was not familiar enough with the MAX
keyword.  Malcolm

Tony Scott wrote:
Show quote
>Malcolm,
>
>From the sounds of it you simply want to return all Customer records from
>the Customer_Info table and the last record from the Payment_Details for the
>customers. If this is the case you can use something similar to this::
>
>SELECT Cus_ID, MAX(Pay_Date) as PayDate
>FROM Customer_Info cus LEFT JOIN Payment_Details pay
>ON cus.Cus_ID pay.Cus_ID
>GROUP BY Cus_ID
>
>Hope this assists,
>
>Tony
>
>> Hi,,
>> I have a simple MS Access database that contains two tables at present.
>[quoted text clipped - 8 lines]
>>
>> Can anyone help me??  Malcolm


AddThis Social Bookmark Button