【问题标题】:Postgresql date difference in hours for a calculated column计算列的 Postgresql 日期差异(以小时为单位)
【发布时间】:2021-10-26 16:06:10
【问题描述】:

您好,我在 Postgresl 中创建了一个视图,除了一列之外,它可以工作。我需要它从之前的reported_date 中减去reported_date,并根据well_id 以小时为单位给出答案。我非常迷茫,我什至不介意花几块钱来解决问题。

COALESCE(g.Delta_Hours, hours_diff*24 + DATE_PART('hour',p.reported_date,'hh,start'-'hh,end'))  AS Delta_Hours
group by p.well_id,
         p.reported_date,

【问题讨论】:

    标签: postgresql datediff


    【解决方案1】:

    使用lag() window function 查找该井的先前报告日期。然后,只需常规减法即可从 2 个日期中得到 INTERVALextract(epoch from <interval>) 得到以秒为单位的增量,最后将 /3600 转换为小时。

    testdb=# create table t(well_id integer, reported_date timestamp);
    insert into t select 1, '2021-08-01 13:00';
    insert into t select 2, '2021-08-01 13:00';
    insert into t select 1, '2021-08-01 15:00';
    insert into t select 2, '2021-08-01 16:00';
    CREATE TABLE
    INSERT 0 1
    INSERT 0 1
    INSERT 0 1
    INSERT 0 1
    testdb=# SELECT well_id, reported_date,
      extract(epoch from (reported_date -
                  (lag(reported_date) over (partition by well_id order by reported_date))
      ))/3600 AS hours_since_last_date_for_this_well
    from t;
     well_id |    reported_date    | hours_since_last_date_for_this_well 
    ---------+---------------------+-------------------------------------
           1 | 2021-08-01 13:00:00 |                                    
           1 | 2021-08-01 15:00:00 |                                   2
           2 | 2021-08-01 13:00:00 |                                    
           2 | 2021-08-01 16:00:00 |                                   3
    (4 rows)
    

    鉴于您在 cmets 中的视图定义,新的视图定义为:

    CREATE OR REPLACE VIEW public.vw_production_with_gauge_time 
    AS
    SELECT p.well_name, p.well_type, p.well_alias, p.is_active, p.latest_production_time, p.id, p.well_id, p.produced_at, p.oil, p.gas, p.water, p.reported_date, COALESCE(g.gauge_time_local, '6:00am'::character varying) AS gauge_time_local, COALESCE(g.gauge_at_local, p.reported_date::date + '06:00:00'::interval) AS gauge_at_local, COALESCE(g.gauge_at, timezone('UTC'::text, timezone('US/Central'::text, p.reported_date::date + '06:00:00'::interval))) AS gauge_at, p.is_operating, p.hours_on, p.normal_hours_on,
    extract(epoch from (p.reported_date -
                  (lag(p.reported_date) over (partition by p.well_id order by p.reported_date))
      ))/3600 AS hours_since_last_date_for_this_well
    FROM vw_production p LEFT JOIN vw_gauge_times g ON p.reported_date::date = g.gauge_at_local::date AND p.well_id = g.well_id ORDER BY p.reported_date DESC;
    
    
    

    【讨论】:

    • 我不会只添加一列来创建表。这似乎不起作用。我会给你完整的视图代码,让你有更好的思路。​​
    • 选择 p.well_name, p.well_type, p.well_alias, p.is_active, p.latest_production_time, p.id, p.well_id, p.produced_at, p.oil, p.gas, p .water, p.reported_date, COALESCE(g.gauge_time_local, '6:00am'::character 变化) AS gauge_time_local, COALESCE(g.gauge_at_local, p.reported_date::date + '06:00:00'::interval) AS gauge_at_local, COALESCE(g.gauge_at, timezone('UTC'::text, timezone('US/Central'::text, p.reported_date::date + '06:00:00'::interval))) AS gauge_at, p.is_operating, p.hours_on,
    • p.is_operating, p.hours_on, p.normal_hours_on 从 vw_production p 左加入 vw_gauge_times g ON p.reported_date::date = g.gauge_at_local::date AND p.well_id = g.well_id 订购p.reported_date DESC; ALTER TABLE public.vw_production_with_gauge_time OWNER TO iwell;将所有表上的 public.vw_production_with_gauge_time 授予 grafana; GRANT SELECT ON TABLE public.vw_production_with_gauge_time 到浮木;将所有表上的 public.vw_production_with_gauge_time 授予 iwell;
    • 创建或替换视图 public.vw_production_with_gauge_time AS
    • 贾斯汀,说“这似乎不起作用”是完全没用的。问题是什么?您是否收到错误 - 如果有,具体是什么?结果不是你所期望的——如果是这样,你到底得到了什么,你期望什么。此外,不要将代码放在 cmets 中,而是用它来更新问题。确保其格式正确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2018-04-02
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多