I have table with data:
create table measure_app_scalemeasurement (
store_code text,
certificate_id text,
audit_ending date);
insert into measure_app_scalemeasurement values
('K010','vwv', '10.12.2023'),
('K010','cert1','12.12.2023'),
('K054','vwv', '14.12.2023'),
('K054','cert1','20.01.2024');
I want to select min audit_ending
date for each market store_code
, also getting its corresponding cert_id
Here’s a query I tried
select store_code, certificate_id, min(audit_ending) from measure_app_scalemeasurement
group by store_code, certificate_id
But it is giving all markets store_code
, and this query:
select distinct on (store_code) store_code, certificate_id, min(audit_ending) from measure_app_scalemeasurement
group by store_code, certificate_id
it gave min date for market K010, but did not for market K054. For K054 it is giving max date.
I want to get first min date by markets with its certificate id
like this:
store_code | certificate_id | audit_ending |
---|---|---|
K010 | vwv | 10.12.2023 |
K054 | vwv | 20.01.2024 |
3
Answers
Use row_number() + filter rn=1:
You can do it using window function
row_number()
to order records by audit_ending per market.You can also use
rank()
instead ofrow_number()
if you have ties and you want to return them all (exemple a market with same two lowest audit_ending ).Demo here
Your
distinct on
was almost right: demoYou just needed a matching
order by
so that for eachstore_code
it only keeps the earliest (min) date.