【问题标题】:Postgres query with non existing dates [closed]不存在日期的 Postgres 查询 [关闭]
【发布时间】:2021-07-19 18:13:47
【问题描述】:

我有一个可以正常工作的查询,但没有选择不存在的日期

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 分组。 但是,一些作业尚未开始,并且在计划表中没有现有日期,并且不会显示在结果中。 如何让这些显示在结果中? 感谢任何帮助。

【问题讨论】:

  • 您没有告诉我们WHERE 条件,它决定了结果中显示的行。

标签: postgresql exists


【解决方案1】:

max() 和 min() 函数忽略 Null 值,因为它们是未知的。

您可以给 null 值默认值,以便 max 和 min 可以比较它们:

select
    j.job_id as "Job No",
    to_char(j.date_created, 'DD/MM/YYYY') as "Created Date",
    to_char(min(coalesce(sc.start_time,date'0001-01-01')), 'DD/MM/YYYY') as "Schedule Start",
    to_char(max(coalesce(sc.end_time,date'9999-12-31')), 'DD/MM/YYYY') as "Schedule End",
.....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 2021-03-25
    • 2017-05-25
    • 2013-08-11
    相关资源
    最近更新 更多