Skip to content
Advertisement

additional condition in a SQL query with min(Date)

I have the code presented below provided to me by one of the members but I do not understand how it acts:

select pdam.*, pr.*
from "DAMPay" as pdam left join lateral
     (select min(pr."AsigStart") as startt
      from "PolsRisc" as pr
      where pdam."PRID" = pr."Index"
     ) pr
     on true;

The code extracts the min of start from another table. I wanna add an aditional condition like:

where pdam."PAYDate" between '2020-06-01' and '2020-06-30'

I cannot insert the condition without an error. Also, I need only to see min(pr.”AsigStart”) from the pr table not all columns (pr.*). Thanks

Advertisement

Answer

I want to add an aditional condition like:

You would typically add a where clause to your existing query:

select pdam.*, pr.*
from "DAMPay" as pdam 
left join lateral (
    select min(pr."AsigStart") as startt
    from "PolsRisc" as pr
    where pdam."PRID" = pr."Index"
) pr on true
where pdam."PAYDate" between '2020-06-01' and '2020-06-30'

Also, I need only to see min(pr."AsigStart") from the pr table not all columns (pr.*)

That’s what the query does already. In the outer query, pr refers to the subquery (not to table PolsRisc). The subquery returns a single column, called startt, so that’s pr.* is actually equivalent to pr.startt.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement