Home > front end >  Postgresql query to select most recent coming date time
Postgresql query to select most recent coming date time

Time:07-30

I have a 2 tables subject_master and slot_master, one subject have multiple slots, I want to run a query that should ruturns the most coming slot , it should retunr the distinct values only e.g . I have following 2 tables

Table1 : subject_master

subject_id subject_name status
UHGAwlfrGDFwU Maths active
X3Sv7nE5HqhXg Physics active
Y7fHY4mEJmHaS Chemistry active

Table 2 : slot_master

slot_id subject_id date start_time
aMkJaKeTMSqRt Y7fHY4mEJmHaS 02-08-2022 06:00
Njpm1BJknxaHb Y7fHY4mEJmHaS 04-08-2022 06:00
XwE8fGvFp8ofI Y7fHY4mEJmHaS 06-08-2022 06:00
J0FnzWAcdNBIb Y7fHY4mEJmHaS 09-08-2022 06:00
7jKgQM9Ly7xB1 Y7fHY4mEJmHaS 11-08-2022 06:00
LaQD4KMgrNfQp Y7fHY4mEJmHaS 13-08-2022 06:00
jOB2zXHhUvzUZ Y7fHY4mEJmHaS 16-08-2022 06:00
wQ8anzVi649H2 Y7fHY4mEJmHaS 18-08-2022 06:00
8koqL52n1rNWX Y7fHY4mEJmHaS 20-08-2022 06:00
5HOMvEBusoSGO X3Sv7nE5HqhXg 01-08-2022 09:00
Nm437BpC2oOoW X3Sv7nE5HqhXg 03-08-2022 09:00
VOs9BTmA4j1ar X3Sv7nE5HqhXg 05-08-2022 09:00
1DCSINyTS80aD X3Sv7nE5HqhXg 08-08-2022 09:00
ihl0sv0RANvbs X3Sv7nE5HqhXg 10-08-2022 09:00
mbAg9pr9PopNW X3Sv7nE5HqhXg 12-08-2022 09:00
VUVJlWCbgo30h X3Sv7nE5HqhXg 15-08-2022 09:00
LAW8p1XTe2GzW X3Sv7nE5HqhXg 17-08-2022 09:00
l9JweYVRhrKiH X3Sv7nE5HqhXg 19-08-2022 09:00
4rhxtLpBIgk60 UHGAwlfrGDFwU 13-08-2022 06:00
7PYANuy3cfI9b UHGAwlfrGDFwU 04-08-2022 06:00
ariHVdxwq9Qlq UHGAwlfrGDFwU 11-08-2022 06:00
eoabdysdnEhgo UHGAwlfrGDFwU 06-08-2022 06:00
l6CPBB8GHVdMz UHGAwlfrGDFwU 09-08-2022 06:00
nMzwARCSb4gun UHGAwlfrGDFwU 02-08-2022 06:00
RotO7tJeYGUks UHGAwlfrGDFwU 16-08-2022 06:00
UXaDInLqulUGp UHGAwlfrGDFwU 18-08-2022 06:00

I want a query that return the expected outoput if current date is 30-07-2022 and Current time is 05:00

subject_id subject_name start_time date
UHGAwlfrGDFwU Maths 06:30 02-08-2022
X3Sv7nE5HqhXg Physics 09:00 01-08-2022
Y7fHY4mEJmHaS Chemistry 06:00 01-08-2022

I am not good in explaning, I am just trying to fetch the most recent coming slot

CodePudding user response:

You can use ROW_NUMBER and an INNER JOIN

WITH CTE As (SELECT 
      "subject_id"
      , "date"
      , "start_time"
      , ROW_NUMBER() OVER(PARTITION BY "subject_id" ORDER BY  "date", "start_time") rn
FROM slot_master 
WHERE DATE("date") > DATE('2022-07-31'))
SELECT       CTE."subject_id"
,subject_name
      , "date"
      , "start_time" FROM CTE  INNER JOIN  subject_master ON cTE.subject_id = subject_master.subject_id
WHERE subject_master.status = 'active' AND rn = 1
subject_id    | subject_name | date       | start_time
:------------ | :----------- | :--------- | :---------
UHGAwlfrGDFwU | Maths        | 02-08-2022 | 06:00     
X3Sv7nE5HqhXg | Physics      | 01-08-2022 | 09:00     
Y7fHY4mEJmHaS | Chemistry    | 02-08-2022 | 06:00     

db<>fiddle here

CodePudding user response:

In PostgreSQL, you can use select distinct on to accomplish this. So long as the date is of type date and start_time is of type time, this will work:

select distinct on (s.subject_id) s.subject_id, s.subject_name, slot.date, slot.start_time
  from subject_master s
       join slot_master slot
         on slot.subject_id = s.subject_id
 where slot.date   slot.start_time >= '2022-07-30 05:00'
 order by s.subject_id, slot.date, slot.start_time;
  • Related