Received: from postgresql.org.org (webmail.postgresql.org [216.126.85.28]) by postgresql.org (8.11.3/8.11.1) with SMTP id f6GGd5a03707 for ; Mon, 16 Jul 2001 12:39:05 -0400 (EDT) (envelope-from pgsql-sql-owner@postgresql.org) Received: from news.tht.net (news.hub.org [216.126.91.242]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f6EMUsa32312; Sat, 14 Jul 2001 18:30:54 -0400 (EDT) (envelope-from news@news.tht.net) Received: (from news@localhost) by news.tht.net (8.11.4/8.11.4) id f6EMTYd27124; Sat, 14 Jul 2001 18:29:34 -0400 (EDT) (envelope-from news) From: "Tom Moreau" References: Subject: Re: Help with SQL statement - Thanks Date: Sat, 14 Jul 2001 18:26:41 -0400 Lines: 65 X-Newsreader: Microsoft Outlook Express 4.72.3612.1700 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3719.2500 Message-ID: X-Newsgroups: comp.database.ms-sqlserver, comp.databases.ms-sqlserver, comp.databases.postgresql.questions, comp.databases.postgresql.sql, microsoft.public.sqlserver.programming To: pgsql-general@postgresql.org.pgsql-sql@postgresql.org X-Archive-Number: 200107/204 X-Sequence-Number: 3999 For this, you need a derived table: select from Payments p join ( select CustomerNo , Date = max (Date) from Payments group by CustomerNo ) as x on x.CustomerNo = p.CustomerNo and p.Date = x.Date -- Tom ---------------------------------------------------------- Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCT SQL Server MVP Columnist, SQL Server Professional Toronto, ON Canada www.pinnaclepublishing.com/sql www.apress.com --- Henry wrote in message ... There is a table called "Payments" which records every payment that a company has received: CustomerNo Date Amount ---------------------------------------- 3214 2/12 30 3214 4/10 50 9943 4/18 40 9943 5/10 30 9943 2/13 70 1121 5/23 10 1121 4/20 40 1121 3/12 30 (more data...) I want to be able to pull out the last payment made by everyone in a query: CustomerNo Date Amount --------------------------------------- 3214 4/10 50 9943 5/10 30 1121 5/23 10 (other users...) How should I write the SQL statement? Please email to me at henry_lu@hotmail.com. Thank you very much. Henry