pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Help with SQL statement - Thanks
3+ messages / 3 participants
[nested] [flat]

* Help with SQL statement - Thanks
@ 2001-07-14 19:16 Henry <henry_lu@hotmail.com>
  2001-07-14 22:26 ` Re: Help with SQL statement - Thanks Tom Moreau <tom@dont.spam.me.cips.ca>
  2001-07-16 18:31 ` Re: Help with SQL statement - Thanks James Orr <james@lrgmail.com>
  0 siblings, 2 replies; 3+ messages in thread

From: Henry @ 2001-07-14 19:16 UTC (permalink / raw)
  To: pgsql-general@postgresql.org.pgsql-sql

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






^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Help with SQL statement - Thanks
  2001-07-14 19:16 Help with SQL statement - Thanks Henry <henry_lu@hotmail.com>
@ 2001-07-14 22:26 ` Tom Moreau <tom@dont.spam.me.cips.ca>
  1 sibling, 0 replies; 3+ messages in thread

From: Tom Moreau @ 2001-07-14 22:26 UTC (permalink / raw)
  To: pgsql-general@postgresql.org.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@hotmail.com. Thank you very much.

Henry








^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Help with SQL statement - Thanks
  2001-07-14 19:16 Help with SQL statement - Thanks Henry <henry_lu@hotmail.com>
@ 2001-07-16 18:31 ` James Orr <james@lrgmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: James Orr @ 2001-07-16 18:31 UTC (permalink / raw)
  To: pgsql-sql

SELECT * FROM Payments P1 WHERE Date = (SELECT MAX(Date) FROM Payments P2
WHERE P2.CustomerNo = P1.CustomerNo)

I think that will do it.
- James

----- Original Message -----
From: "Henry" <henry_lu@hotmail.com>
To: <pgsql-general@postgresql.org.pgsql-sql@postgresql.org>
Sent: Saturday, July 14, 2001 3:16 PM
Subject: [SQL] Help with SQL statement - Thanks


> 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
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://www.postgresql.org/search.mpl
>




^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2001-07-16 18:31 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-07-14 19:16 Help with SQL statement - Thanks Henry <henry_lu@hotmail.com>
2001-07-14 22:26 ` Tom Moreau <tom@dont.spam.me.cips.ca>
2001-07-16 18:31 ` James Orr <james@lrgmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox