Re: left outer join to pull in most recent record

From: "MS (direkt)" <martin(dot)stoecker(at)stb-datenservice(dot)de>
To: pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: Re: left outer join to pull in most recent record
Date: 2018-10-10 09:13:15
Message-ID: 5abe34aa-b5a3-7ad4-7e62-960ab86e201e@stb-datenservice.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

select distinct s_regno, max(s_stock_no) over (partition by s_regno
order by s_regno) from stock;

Am 09.10.2018 um 15:18 schrieb Gary Stainburn:
> I have the following stock list table.
>
> # select * from stock;
> s_stock_no | s_regno
> ------------+---------
> N12345 | GPS1
> N12346 | TEST1
> U123 | GPS1
> (3 rows)
>
> This shows two vehicles.
> GPS1 was originally a new vehicle which we then sold.
> TEST1 is also a new vehicle
> GPS1 is sunsequently purchased back from the customer as a part exchange, and
> is put on the stock table as a used car.
>
> If I later want to do a search based on the registration number, wishing to
> retrieve the most recent record what is the *best* (quickest or least CPU
> time depending on your preference) way to do this?
>
> By most recent record, I mean the record with the highest stock number.
>
> I'm looking for a solution to do a straight select where.....
> but I am also looking for a solution that can be used in a left join, for
> example
>
> select diary.*, stock.*
> from diary
> left outer join stock on ...........
>

--

Widdersdorfer Str. 415, 50933 Köln; Tel. +49 / 221 / 9544 010
HRB Köln HRB 75439, Geschäftsführer: S. Böhland, S. Rosenbauer

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Stanton Schmidt 2018-10-10 11:51:30 Re: pg_dumpall and restore
Previous Message Thomas Kellerer 2018-10-09 20:48:37 Re: remove from list?