不存在日期的Postgresql查询



我有一个查询工作正常,但不会拾取不存在的日期

select
j.job_id as "Job No",
c.business_name as "Customer",
to_char(j.date_created, 'DD/MM/YYYY') as "Created Date",
to_char(min(sc.start_time), 'DD/MM/YYYY') as "Schedule Start",
to_char(max(sc.end_time), 'DD/MM/YYYY') as "Schedule End",
to_char(j.date_due, 'DD/MM/YYYY') as "Due Date"
from sampleco_jobs j
join sampleco_customers c
on j.customer_id = c.customer_id
join sampleco_schedules sc
on j.job_id = sc.job_id
where c.customer_id = 29726
group by j.job_id, c.business_name;

每个作业可能有多个计划日期,查询将查找最早和最年轻的计划日期,并按job_id进行分组。但是,有些作业尚未开始,并且在时间表表中没有现有日期,并且不会显示在结果中。如何让所有工作都显示在结果中?还可能添加类似于";没有时间表";代替空白感谢任何帮助。

似乎只需要使用时间表表执行left join

select
j.job_id as "Job No",
c.business_name as "Customer",
to_char(j.date_created, 'DD/MM/YYYY') as "Created Date",
to_char(min(sc.start_time), 'DD/MM/YYYY') as "Schedule Start",
to_char(max(sc.end_time), 'DD/MM/YYYY') as "Schedule End",
to_char(j.date_due, 'DD/MM/YYYY') as "Due Date"
from sampleco_jobs j
join sampleco_customers c
on j.customer_id = c.customer_id
left join sampleco_schedules sc --< left join here
on j.job_id = sc.job_id
where c.customer_id = 29726
group by j.job_id, c.business_name;

最新更新