如何检查查询是否有值并在 postgres SQL 的间隔中获取开始日期和结束日期的差异?

How to check the query has values and get the difference of the start date and end date in intervals in postgres SQL?

这些是问题摘要:

Viz: Start date :1-1-2021, End date : 1-12-2021 Months: 1-1-2021 1-2-2021 1-3-2021 .... 12-12-2021

// To check if any values are there
        $query_check = pg_query(DBCON,"select id , (COALESCE(revised_cost,administrative_sanction_project_cost,award_tender_project_cost) as cost, COALESCE(award_tender_contract_date,administrative_sanction_date,actual_start_date) as start_date, COALESCE(revised_completion_date,scheduled_completion_date)::date) as end_date from sp_index_v4 where id = $proj_id ");
        // If it has no null values then execute the logic - Autoset of target progress before editing it.
        if ($query_check != null) {
            console.log ('Hi'+$query_check);
        $query_cost = pg_query(DBCON,"select id ,(COALESCE(revised_cost,administrative_sanction_project_cost,award_tender_project_cost)/ datediff('month',COALESCE(award_tender_contract_date,administrative_sanction_date,actual_start_date)::date, COALESCE(revised_completion_date,scheduled_completion_date)::date)) as cost_of_one_month, TRUNC(((COALESCE(revised_cost,administrative_sanction_project_cost,award_tender_project_cost)/ datediff('month',COALESCE(award_tender_contract_date,administrative_sanction_date,actual_start_date)::date, COALESCE(revised_completion_date,scheduled_completion_date)::date))/ (COALESCE(revised_cost,administrative_sanction_project_cost,award_tender_project_cost)))*100,2) as cost_percent from sp_index_v4  id = $proj_id");
    // Insert for new project entry update for already having project
            $ins_query = "insert into project_target_progress(project_id,cum_financial_progress_in_cr,cum_financial_progress_in_percent,cum_physical_progress_in_percent,target_progress_date)values('$id',cost_of_one_month,cost_percent,cost_percent,start_date)";
            $insert_data = pg_query(DBCON, $ins_query);
}
        else {
            echo "Value cannot be inserted";
            console.log("inside the else block");
        }

我用不同的机会编写了自己的 datediff 函数:

create or replace function datediff
(
    units varchar(30), 
    start_t timestamp, 
    end_t timestamp
) 
returns int as $$
declare
     diff_interval interval; 
     diff int = 0;
     years_diff int = 0;
begin
    
    if units in ('yy', 'yyyy', 'year', 'mm', 'm', 'month') then
        years_diff = date_part('year', end_t) - date_part('year', start_t);
    
        if units in ('yy', 'yyyy', 'year') then
            return years_diff;
        else
            return years_diff * 12 + (date_part('month', end_t) - date_part('month', start_t)); 
        end if;
    end if;
   
    diff_interval = end_t - start_t;
    diff = diff + date_part('day', diff_interval);
 
    if units in ('wk', 'ww', 'week') then
        diff = diff/7;
        return diff;
    end if;
 
    if units in ('dd', 'd', 'day') then
        return diff;
    end if;
 
    diff = diff * 24 + date_part('hour', diff_interval); 
 
    if units in ('hh', 'hour') then
        return diff;
    end if;
 
    diff = diff * 60 + date_part('minute', diff_interval);
 
    if units in ('mi', 'n', 'minute') then
        return diff;
    end if;
 
    diff = diff * 60 + date_part('second', diff_interval);
    return diff;

end;
$$ language plpgsql;

现在,我们可以使用我们的函数编写示例查询:

select 
    mm.start_date, 
    mm.end_date, 
    test.datediff('year', mm.start_date, mm.end_date) as diff_year,
    test.datediff('month', mm.start_date, mm.end_date) as diff_month,
    test.datediff('day', mm.start_date, mm.end_date) as diff_day
from (
    select '2021-11-25 02:20:54.200'::timestamp as start_date, now()::timestamp as end_date 
) mm

结果:

start_date end_date diff_year diff_month diff_day
2021-11-25 02:20:54.200 2022-02-24 06:10:52.258 1 3 91