Create a SQL query for Postgres to get latest two rows for every foreign key
Budget: €8 – €30 EUR
Hello all,
I have three tables that need to be joined and then get 2 or more entries as result. I have already created the query for getting one result per id of one table.
Select devprojects_scheme.id as project_id, devprojects_scheme.label, devprojects_scheme.objective, responsible, status, workedtimelastthreemonth, jtbd, achievements, b.id as projectupdate_id, roadblock, progressupdate, nextsteps, milestones, created_at from devprojects_scheme RIGHT JOIN (
SELECT project_id, COALESCE(sum(upper(tsrange(starttime,endtime)) - lower(tsrange(starttime,endtime)))*userhourweight.cost_weight*0.01, interval '0 minute')
as workedtimelastthreemonth from tasks
left join userhourweight on tasks.user_name=userhourweight.user_name
WHERE starttime >= current_date - interval '3' month
and starttime < current_date GROUP BY project_id, userhourweight.cost_weight
) as d on devprojects_scheme.id = d.project_id
left join (
select distinct on (project_id) * from projectupdates WHERE created_at IS NOT NULL
order by project_id, created_at desc
) as b on devprojects_scheme.id = b.project_id
I have three tables that need to be joined and then get 2 or more entries as result. I have already created the query for getting one result per id of one table.
Select devprojects_scheme.id as project_id, devprojects_scheme.label, devprojects_scheme.objective, responsible, status, workedtimelastthreemonth, jtbd, achievements, b.id as projectupdate_id, roadblock, progressupdate, nextsteps, milestones, created_at from devprojects_scheme RIGHT JOIN (
SELECT project_id, COALESCE(sum(upper(tsrange(starttime,endtime)) - lower(tsrange(starttime,endtime)))*userhourweight.cost_weight*0.01, interval '0 minute')
as workedtimelastthreemonth from tasks
left join userhourweight on tasks.user_name=userhourweight.user_name
WHERE starttime >= current_date - interval '3' month
and starttime < current_date GROUP BY project_id, userhourweight.cost_weight
) as d on devprojects_scheme.id = d.project_id
left join (
select distinct on (project_id) * from projectupdates WHERE created_at IS NOT NULL
order by project_id, created_at desc
) as b on devprojects_scheme.id = b.project_id