|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
SQL Query - Isolating last row in customer recordHi,,
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 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 > 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 -- Message posted via SQLMonster.com http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server-programming/200602/1 |
|||||||||||||||||||||||