pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedSelecting table row with latest date
23+ messages / 6 participants
[nested] [flat]
* Selecting table row with latest date
@ 2021-08-19 14:37 Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:40 ` Re: Selecting table row with latest date David G. Johnston <david.g.johnston@gmail.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Tom Lane <tgl@sss.pgh.pa.us>
2021-08-19 14:42 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
0 siblings, 4 replies; 23+ messages in thread
From: Rich Shepard @ 2021-08-19 14:37 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
I have a table of contacts (PK is the person_nbr and contact_date) and I
want to select only the row with the latest (most recent) contact_date. The
Comparison Date/Time sections in the postgres 12 doc doesn't appear to have
what I want, and when I try to use the max() aggregate function it throws an
error.
Please point me to the proper place in the docs where I can learn how to do
this.
Rich
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
@ 2021-08-19 14:40 ` David G. Johnston <david.g.johnston@gmail.com>
2021-08-19 14:45 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
3 siblings, 1 reply; 23+ messages in thread
From: David G. Johnston @ 2021-08-19 14:40 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thursday, August 19, 2021, Rich Shepard <rshepard@appl-ecosys.com> wrote:
> I have a table of contacts (PK is the person_nbr and contact_date)
>
Select distinct on (person_nbr) ….. order by person_nbr, contact_date desc;
David J.
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:40 ` Re: Selecting table row with latest date David G. Johnston <david.g.johnston@gmail.com>
@ 2021-08-19 14:45 ` Rich Shepard <rshepard@appl-ecosys.com>
0 siblings, 0 replies; 23+ messages in thread
From: Rich Shepard @ 2021-08-19 14:45 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thu, 19 Aug 2021, David G. Johnston wrote:
> Select distinct on (person_nbr) ….. order by person_nbr, contact_date
> desc;
David,
Please clarify: would this produce the most recent contact_date for each
person_nbr? The manual reads that two rows (e.g., for the same person_nbr)
are considered distinct if they differ in any one column (e.g.,
contact_dates).
Still confused.
Rich
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
@ 2021-08-19 14:41 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:49 ` Selecting table row with latest date David G. Johnston <david.g.johnston@gmail.com>
3 siblings, 1 reply; 23+ messages in thread
From: Rich Shepard @ 2021-08-19 14:41 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On Thu, 19 Aug 2021, Rich Shepard wrote:
> Please point me to the proper place in the docs where I can learn how to do
> this.
If I use DISTINCT ON would this produce the most recent date for each
person_nbr?
Rich
^ permalink raw reply [nested|flat] 23+ messages in thread
* Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
@ 2021-08-19 14:49 ` David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 23+ messages in thread
From: David G. Johnston @ 2021-08-19 14:49 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thursday, August 19, 2021, Rich Shepard <rshepard@appl-ecosys.com> wrote:
> On Thu, 19 Aug 2021, Rich Shepard wrote:
>
> Please point me to the proper place in the docs where I can learn how to do
>> this.
>>
>
> If I use DISTINCT ON would this produce the most recent date for each
> person_nbr?
>
Yes, I mis-read your request and thought you wanted the most recent per
contact. Frankly, the single most recent is too simple that my mind didn’t
register that possibility.
David J.
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
@ 2021-08-19 14:41 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-08-19 14:47 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 15:39 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
3 siblings, 2 replies; 23+ messages in thread
From: Tom Lane @ 2021-08-19 14:41 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general@lists.postgresql.org
Rich Shepard <rshepard@appl-ecosys.com> writes:
> I have a table of contacts (PK is the person_nbr and contact_date) and I
> want to select only the row with the latest (most recent) contact_date. The
> Comparison Date/Time sections in the postgres 12 doc doesn't appear to have
> what I want, and when I try to use the max() aggregate function it throws an
> error.
The best way is usually like
select * from mytable order by contact_date desc limit 1;
If you have an index on contact_date this should work very well indeed.
regards, tom lane
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Tom Lane <tgl@sss.pgh.pa.us>
@ 2021-08-19 14:47 ` Rich Shepard <rshepard@appl-ecosys.com>
1 sibling, 0 replies; 23+ messages in thread
From: Rich Shepard @ 2021-08-19 14:47 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On Thu, 19 Aug 2021, Tom Lane wrote:
> The best way is usually like
>
> select * from mytable order by contact_date desc limit 1;
>
> If you have an index on contact_date this should work very well indeed.
Tom,
I don't have an index on that table. I'll add one and try you suggestion.
Thanks,
Rich
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Tom Lane <tgl@sss.pgh.pa.us>
@ 2021-08-19 15:39 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 15:52 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
1 sibling, 1 reply; 23+ messages in thread
From: Rich Shepard @ 2021-08-19 15:39 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On Thu, 19 Aug 2021, Tom Lane wrote:
> The best way is usually like
> select * from mytable order by contact_date desc limit 1;
> If you have an index on contact_date this should work very well indeed.
tom,
I added an index on contact_date and the query returned only one row. Huh!
Not what I expected.
This is the script I need to fine-tune (and I've forgotten the role of sq
since someone suggested it a few years ago):
----------
/* This query selects all whose next_contact date is today or earlier; no nulls.
This version should select the most recent contact_date by person_nbr,
order by person_nbr and next_contact date. STILL NEEDS WORK.
*/
select p.person_nbr, p.lname, p.fname, p.direct_phone, p.cell_phone, o.company_name, sq.*
from people as p
join companies as o on p.company_nbr = o.company_nbr
cross join
lateral
(select *
from contacts as a
where a.person_nbr = p.person_nbr and
a.next_contact <= current_date and
a.next_contact is not null
order by person_nbr, a.next_contact ASC
) sq
order by sq.next_contact ASC;
----------
Rich
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Tom Lane <tgl@sss.pgh.pa.us>
2021-08-19 15:39 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
@ 2021-08-19 15:52 ` Adrian Klaver <adrian.klaver@aklaver.com>
2021-08-19 16:06 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
0 siblings, 1 reply; 23+ messages in thread
From: Adrian Klaver @ 2021-08-19 15:52 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; pgsql-general@lists.postgresql.org
On 8/19/21 8:39 AM, Rich Shepard wrote:
> On Thu, 19 Aug 2021, Tom Lane wrote:
>
>> The best way is usually like
>> select * from mytable order by contact_date desc limit 1;
>> If you have an index on contact_date this should work very well indeed.
>
> tom,
>
> I added an index on contact_date and the query returned only one row. Huh!
> Not what I expected.
>
> This is the script I need to fine-tune (and I've forgotten the role of sq
> since someone suggested it a few years ago):
>
> ----------
> /* This query selects all whose next_contact date is today or earlier;
> no nulls.
> This version should select the most recent contact_date by person_nbr,
> order by person_nbr and next_contact date. STILL NEEDS WORK.
> */
>
> select p.person_nbr, p.lname, p.fname, p.direct_phone, p.cell_phone,
> o.company_name, sq.*
> from people as p
> join companies as o on p.company_nbr = o.company_nbr
> cross join
> lateral
> (select *
> from contacts as a
> where a.person_nbr = p.person_nbr and
> a.next_contact <= current_date and
> a.next_contact is not null
> order by person_nbr, a.next_contact ASC
> ) sq
> order by sq.next_contact ASC;
> ----------
Alright now I am confused. You keep referring to contact_date, yet the
query is referring to next_contact. Are they the same thing, different
things or other?
>
> Rich
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Tom Lane <tgl@sss.pgh.pa.us>
2021-08-19 15:39 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 15:52 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
@ 2021-08-19 16:06 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:27 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
2021-08-19 16:53 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
0 siblings, 2 replies; 23+ messages in thread
From: Rich Shepard @ 2021-08-19 16:06 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On Thu, 19 Aug 2021, Adrian Klaver wrote:
> Alright now I am confused. You keep referring to contact_date, yet the
> query is referring to next_contact. Are they the same thing, different
> things or other?
Adrian,
The table has 5 columns: person_nbr, contact_date, contact_type, notes, and
next_contact.
I want the query to find all person_nbr whose most recent contact_date has a
next_contact date <= today. I don't need prior contact_dates and their
next_contact dates because some go back several years. I want to know those
I need to contact again based on our most recent contact.
HTH,
Rich
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Tom Lane <tgl@sss.pgh.pa.us>
2021-08-19 15:39 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 15:52 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
2021-08-19 16:06 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
@ 2021-08-19 16:27 ` Rob Sargent <robjsargent@gmail.com>
2021-08-19 16:31 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
1 sibling, 1 reply; 23+ messages in thread
From: Rob Sargent @ 2021-08-19 16:27 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>
> On Aug 19, 2021, at 10:06 AM, Rich Shepard <rshepard@appl-ecosys.com> wrote:
>
> On Thu, 19 Aug 2021, Adrian Klaver wrote:
>
>> Alright now I am confused. You keep referring to contact_date, yet the
>> query is referring to next_contact. Are they the same thing, different
>> things or other?
>
> Adrian,
>
> The table has 5 columns: person_nbr, contact_date, contact_type, notes, and
> next_contact.
>
> I want the query to find all person_nbr whose most recent contact_date has a
> next_contact date <= today. I don't need prior contact_dates and their
> next_contact dates because some go back several years. I want to know those
> I need to contact again based on our most recent contact.
>
> HTH,
>
Did you try David J’s suggestion? or maybe
select person_nbr, max(next_contact) group by person_nbr where next_contact < now();
A table with person_nbr (pk), next_contact would make this much easier. Seems to me a person can only have one next-contact? (as opposed to all future_contact)
> Rich
>
>
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Tom Lane <tgl@sss.pgh.pa.us>
2021-08-19 15:39 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 15:52 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
2021-08-19 16:06 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:27 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
@ 2021-08-19 16:31 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:39 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
0 siblings, 1 reply; 23+ messages in thread
From: Rich Shepard @ 2021-08-19 16:31 UTC (permalink / raw)
To: pgsql-general <pgsql-general@lists.postgresql.org>
On Thu, 19 Aug 2021, Rob Sargent wrote:
> Did you try David J’s suggestion? or maybe
Rob,
Yes.
> select person_nbr, max(next_contact) group by person_nbr where
> next_contact < now();
> A table with person_nbr (pk), next_contact would make this much easier.
> Seems to me a person can only have one next-contact? (as opposed to all
> future_contact)
The peoples table has person_nbr as the PK. The contacts table has multiple
rows for all contacts with that person_nbr. The contacts table has a
multicolum PK: person_nbr and contact_date as there is only one contact on
any given day for that person.
HTH,
Rich
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Tom Lane <tgl@sss.pgh.pa.us>
2021-08-19 15:39 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 15:52 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
2021-08-19 16:06 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:27 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
2021-08-19 16:31 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
@ 2021-08-19 16:39 ` Rob Sargent <robjsargent@gmail.com>
2021-08-19 16:59 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
0 siblings, 1 reply; 23+ messages in thread
From: Rob Sargent @ 2021-08-19 16:39 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>
> On Aug 19, 2021, at 10:31 AM, Rich Shepard <rshepard@appl-ecosys.com> wrote:
>
> On Thu, 19 Aug 2021, Rob Sargent wrote:
>
>> Did you try David J’s suggestion? or maybe
>
> Rob,
>
> Yes.
>
>> select person_nbr, max(next_contact) group by person_nbr where
>> next_contact < now();
>
>> A table with person_nbr (pk), next_contact would make this much easier.
>> Seems to me a person can only have one next-contact? (as opposed to all
>> future_contact)
>
> The peoples table has person_nbr as the PK. The contacts table has multiple
> rows for all contacts with that person_nbr. The contacts table has a
> multicolum PK: person_nbr and contact_date as there is only one contact on
> any given day for that person.
>
> HTH,
>
> Rich
>
Yeah, but my quibble is the the table you described up-thread. Your contact table contains next_contact? I think that column should be normalized out.
Using David’s trick as a CTE should be descent starting point for you ultimate result?
>
>
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Tom Lane <tgl@sss.pgh.pa.us>
2021-08-19 15:39 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 15:52 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
2021-08-19 16:06 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:27 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
2021-08-19 16:31 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:39 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
@ 2021-08-19 16:59 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 17:00 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
2021-08-20 07:22 ` Re: Selecting table row with latest date Francisco Olarte <folarte@peoplecall.com>
0 siblings, 2 replies; 23+ messages in thread
From: Rich Shepard @ 2021-08-19 16:59 UTC (permalink / raw)
To: pgsql-general <pgsql-general@lists.postgresql.org>
On Thu, 19 Aug 2021, Rob Sargent wrote:
> Yeah, but my quibble is the the table you described up-thread. Your
> contact table contains next_contact? I think that column should be
> normalized out.
Rob,
Why should I have a separate table with one column: next_contact? The
next_contact date is associated with a person and the date that person was
contacted.
Rich
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Tom Lane <tgl@sss.pgh.pa.us>
2021-08-19 15:39 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 15:52 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
2021-08-19 16:06 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:27 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
2021-08-19 16:31 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:39 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
2021-08-19 16:59 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
@ 2021-08-19 17:00 ` Rob Sargent <robjsargent@gmail.com>
2021-08-19 17:20 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
1 sibling, 1 reply; 23+ messages in thread
From: Rob Sargent @ 2021-08-19 17:00 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>
> On Aug 19, 2021, at 10:59 AM, Rich Shepard <rshepard@appl-ecosys.com> wrote:
>
> On Thu, 19 Aug 2021, Rob Sargent wrote:
>
>> Yeah, but my quibble is the the table you described up-thread. Your
>> contact table contains next_contact? I think that column should be
>> normalized out.
>
> Rob,
>
> Why should I have a separate table with one column: next_contact? The
> next_contact date is associated with a person and the date that person was
> contacted.
>
> Rich
sorry, wasn’t clear: person_nbr, next_contact
On the premise that there can only be one next date.
>
>
>
>
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Tom Lane <tgl@sss.pgh.pa.us>
2021-08-19 15:39 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 15:52 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
2021-08-19 16:06 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:27 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
2021-08-19 16:31 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:39 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
2021-08-19 16:59 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 17:00 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
@ 2021-08-19 17:20 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 17:25 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
0 siblings, 1 reply; 23+ messages in thread
From: Rich Shepard @ 2021-08-19 17:20 UTC (permalink / raw)
To: pgsql-general <pgsql-general@lists.postgresql.org>
On Thu, 19 Aug 2021, Rob Sargent wrote:
> sorry, wasn’t clear: person_nbr, next_contact
> On the premise that there can only be one next date.
Rob,
I wasn't sufficiently clear. The contacts table has a row for each
(person_nbr, contact_date). For each row there's also either a next_contact
date or that column is null (e.g., pandemic-related no longer at that
company).
Regards,
Rich
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Tom Lane <tgl@sss.pgh.pa.us>
2021-08-19 15:39 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 15:52 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
2021-08-19 16:06 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:27 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
2021-08-19 16:31 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:39 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
2021-08-19 16:59 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 17:00 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
2021-08-19 17:20 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
@ 2021-08-19 17:25 ` Rob Sargent <robjsargent@gmail.com>
0 siblings, 0 replies; 23+ messages in thread
From: Rob Sargent @ 2021-08-19 17:25 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>
> On Aug 19, 2021, at 11:20 AM, Rich Shepard <rshepard@appl-ecosys.com> wrote:
>
> On Thu, 19 Aug 2021, Rob Sargent wrote:
>
>> sorry, wasn’t clear: person_nbr, next_contact
>> On the premise that there can only be one next date.
>
> Rob,
>
> I wasn't sufficiently clear. The contacts table has a row for each
> (person_nbr, contact_date). For each row there's also either a next_contact
> date or that column is null (e.g., pandemic-related no longer at that
> company).
>
Yeah, I get that each person may have been contacted more than once for a history of contacting. I just feel that “next_contact” doesn’t belong there.
> Regards,
>
> Rich
>
>
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Tom Lane <tgl@sss.pgh.pa.us>
2021-08-19 15:39 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 15:52 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
2021-08-19 16:06 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:27 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
2021-08-19 16:31 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:39 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
2021-08-19 16:59 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
@ 2021-08-20 07:22 ` Francisco Olarte <folarte@peoplecall.com>
2021-08-20 12:37 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
1 sibling, 1 reply; 23+ messages in thread
From: Francisco Olarte @ 2021-08-20 07:22 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Rich:
On Thu, Aug 19, 2021 at 6:59 PM Rich Shepard <rshepard@appl-ecosys.com> wrote:
> On Thu, 19 Aug 2021, Rob Sargent wrote:
> > Yeah, but my quibble is the the table you described up-thread. Your
> > contact table contains next_contact? I think that column should be
> > normalized out.
> Why should I have a separate table with one column: next_contact? The
> next_contact date is associated with a person and the date that person was
> contacted.
Is your next_contact really dependent on the contact record (
person+contact_date? ).
I mean, your tables seem CRM like. You stated next_contact for old
contacts is not interesting. It seems next_contact is associated just
by a person, something like this:
You contact person 1 on april 2, decide to follow up on june 2 =>
insert ( p=1, cd=20210402, nc=20210602 ).
Case 1: You contact it in 20210603 , followup for mid july:
Insert (1, 20210603, 20210715).
Is the nc for the previous record useful for anything ? ( It seems
to be nor useful for calling, but you may want to do reports to see
next-contact correlation with the contacts on another records )
Case 2: You contact in mid may ( for whatever reason, he calls you,
whatever, you insert a record for follow up in July ( 1,20210515,
20210715).
Is the next contact date for june still valid?
To me, and maybe others, it seems there is only one valid next-contact
date, potentially null, which should go in the person record, and may
be the contacts table should capture the value of next-contact at the
time the contact was made for analysis. This also makes finding
contacts to be made soon easier.
Of course no separate table for next-contact.
Francisco Olarte.
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Tom Lane <tgl@sss.pgh.pa.us>
2021-08-19 15:39 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 15:52 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
2021-08-19 16:06 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:27 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
2021-08-19 16:31 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:39 ` Re: Selecting table row with latest date Rob Sargent <robjsargent@gmail.com>
2021-08-19 16:59 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-20 07:22 ` Re: Selecting table row with latest date Francisco Olarte <folarte@peoplecall.com>
@ 2021-08-20 12:37 ` Rich Shepard <rshepard@appl-ecosys.com>
0 siblings, 0 replies; 23+ messages in thread
From: Rich Shepard @ 2021-08-20 12:37 UTC (permalink / raw)
To: pgsql-general <pgsql-general@lists.postgresql.org>
On Fri, 20 Aug 2021, Francisco Olarte wrote:
> Is your next_contact really dependent on the contact record (
> person+contact_date? ).
Francisco,
Yes.
> I mean, your tables seem CRM like. You stated next_contact for old
> contacts is not interesting. It seems next_contact is associated just
> by a person, something like this:
Not true. The history of contacts is highly valuable.
> Is the nc for the previous record useful for anything ? ( It seems to be
> nor useful for calling, but you may want to do reports to see next-contact
> correlation with the contacts on another records )
Yes.
> To me, and maybe others, it seems there is only one valid next-contact
> date, potentially null, which should go in the person record, and may be
> the contacts table should capture the value of next-contact at the time
> the contact was made for analysis. This also makes finding contacts to be
> made soon easier.
That's because you don't know my business: I'm currently in my twenty-ninth
year as a consultant helping clients avoid and resolve environmental issues
and have long ago found what information I need and use to efficiently run
my business.
Regards,
Rich
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Tom Lane <tgl@sss.pgh.pa.us>
2021-08-19 15:39 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 15:52 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
2021-08-19 16:06 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
@ 2021-08-19 16:53 ` Adrian Klaver <adrian.klaver@aklaver.com>
2021-08-19 17:17 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
1 sibling, 1 reply; 23+ messages in thread
From: Adrian Klaver @ 2021-08-19 16:53 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; pgsql-general@lists.postgresql.org
On 8/19/21 9:06 AM, Rich Shepard wrote:
> On Thu, 19 Aug 2021, Adrian Klaver wrote:
>
>> Alright now I am confused. You keep referring to contact_date, yet the
>> query is referring to next_contact. Are they the same thing, different
>> things or other?
>
> Adrian,
>
> The table has 5 columns: person_nbr, contact_date, contact_type, notes, and
> next_contact.
>
> I want the query to find all person_nbr whose most recent contact_date
> has a
> next_contact date <= today. I don't need prior contact_dates and their
> next_contact dates because some go back several years. I want to know those
> I need to contact again based on our most recent contact.
So take David Johnston's query:
Select distinct on (person_nbr) ….. order by person_nbr, contact_date desc;
and add the next_contact filter:
Select distinct on (person_nbr) * from contacts where next_contact <=
current_date order by person_nbr, contact_date desc;
See if that returns what you want from the contacts table. Then you can
attach the other information to it.
>
> HTH,
>
> Rich
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Tom Lane <tgl@sss.pgh.pa.us>
2021-08-19 15:39 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 15:52 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
2021-08-19 16:06 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:53 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
@ 2021-08-19 17:17 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 17:38 ` Re: Selecting table row with latest date David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 1 reply; 23+ messages in thread
From: Rich Shepard @ 2021-08-19 17:17 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On Thu, 19 Aug 2021, Adrian Klaver wrote:
> So take David Johnston's query:
> Select distinct on (person_nbr) ….. order by person_nbr, contact_date desc;
Adrian,
contact_date
--------------
2021-08-17
2019-05-14
2019-05-15
2021-08-17
2018-04-05
2021-08-17
2018-04-05
2021-07-23
2019-04-01
2019-05-10
2019-03-15
2021-08-17
2019-04-16
2019-05-15
2021-08-17
2019-05-29
2021-08-17
2019-05-29
2021-07-23
2021-08-12
...
> and add the next_contact filter:
>
> Select distinct on (person_nbr) * from contacts where next_contact <=
> current_date order by person_nbr, contact_date desc;
Select distinct on (p.person_nbr) * from c.contacts
from people as p, contacts as c
where cnext_contact <= current_date and
p.person_nbr = c.person_nbr
order by p.person_nbr, c.contact_date desc;
$ psql -d bustrac -f test_query.sql
psql:test_query.sql:5: ERROR: syntax error at or near "from"
LINE 2: from people as p, contacts as c
^
I still am missing quite a bit.
Rich
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Re: Selecting table row with latest date Tom Lane <tgl@sss.pgh.pa.us>
2021-08-19 15:39 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 15:52 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
2021-08-19 16:06 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:53 ` Re: Selecting table row with latest date Adrian Klaver <adrian.klaver@aklaver.com>
2021-08-19 17:17 ` Re: Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
@ 2021-08-19 17:38 ` David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 23+ messages in thread
From: David G. Johnston @ 2021-08-19 17:38 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thursday, August 19, 2021, Rich Shepard <rshepard@appl-ecosys.com> wrote:
> On Thu, 19 Aug 2021, Adrian Klaver wrote:
>
> So take David Johnston's query:
>> Select distinct on (person_nbr) ….. order by person_nbr, contact_date
>> desc;
>>
>
> Adrian,
>
> contact_date --------------
> 2021-08-17
What’s your point?
> Select distinct on (person_nbr) * from contacts where next_contact <=
>> current_date order by person_nbr, contact_date desc;
>
>
> Select distinct on (p.person_nbr) * from c.contacts
> from people as p, contacts as c
> where cnext_contact <= current_date and
> p.person_nbr = c.person_nbr
> order by p.person_nbr, c.contact_date desc;
>
> $ psql -d bustrac -f test_query.sql
> psql:test_query.sql:5: ERROR: syntax error at or near "from"
> LINE 2: from people as p, contacts as c
> ^
>
Yeah, you wrote two from clauses…
David J.
^ permalink raw reply [nested|flat] 23+ messages in thread
* Re: Selecting table row with latest date
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
@ 2021-08-19 14:42 ` Adrian Klaver <adrian.klaver@aklaver.com>
3 siblings, 0 replies; 23+ messages in thread
From: Adrian Klaver @ 2021-08-19 14:42 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; pgsql-general@lists.postgresql.org
On 8/19/21 7:37 AM, Rich Shepard wrote:
> I have a table of contacts (PK is the person_nbr and contact_date) and I
> want to select only the row with the latest (most recent) contact_date. The
> Comparison Date/Time sections in the postgres 12 doc doesn't appear to have
> what I want, and when I try to use the max() aggregate function it
> throws an
> error.
Show the query and the error you received.
>
> Please point me to the proper place in the docs where I can learn how to do
> this.
>
> Rich
>
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 23+ messages in thread
end of thread, other threads:[~2021-08-20 12:37 UTC | newest]
Thread overview: 23+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-08-19 14:37 Selecting table row with latest date Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:40 ` David G. Johnston <david.g.johnston@gmail.com>
2021-08-19 14:45 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:41 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 14:49 ` David G. Johnston <david.g.johnston@gmail.com>
2021-08-19 14:41 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-08-19 14:47 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 15:39 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 15:52 ` Adrian Klaver <adrian.klaver@aklaver.com>
2021-08-19 16:06 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:27 ` Rob Sargent <robjsargent@gmail.com>
2021-08-19 16:31 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:39 ` Rob Sargent <robjsargent@gmail.com>
2021-08-19 16:59 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 17:00 ` Rob Sargent <robjsargent@gmail.com>
2021-08-19 17:20 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 17:25 ` Rob Sargent <robjsargent@gmail.com>
2021-08-20 07:22 ` Francisco Olarte <folarte@peoplecall.com>
2021-08-20 12:37 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 16:53 ` Adrian Klaver <adrian.klaver@aklaver.com>
2021-08-19 17:17 ` Rich Shepard <rshepard@appl-ecosys.com>
2021-08-19 17:38 ` David G. Johnston <david.g.johnston@gmail.com>
2021-08-19 14:42 ` Adrian Klaver <adrian.klaver@aklaver.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