Re: Help with SQL statement - Thanks

From: "Tom Moreau" <tom(at)dont(dot)spam(dot)me(dot)cips(dot)ca>
To: pgsql-general(at)postgresql(dot)org(dot)pgsql-sql(at)postgresql(dot)org
Subject: Re: Help with SQL statement - Thanks
Date: 2001-07-14 22:26:41
Message-ID: OseViQLDBHA.1380@tkmsftngp07
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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(at)hotmail(dot)com(dot) Thank you very much.

Henry

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Christopher Sawtell 2001-07-15 03:43:44 Re: Date Validation?
Previous Message Henry 2001-07-14 19:16:01 Help with SQL statement - Thanks