PostgreSql 查询

1.PostgreSql 查询一个月内的天数

SELECT to_char( generate_series(date_trunc('month',now())+ interval '-1 month',
    date_trunc('month',now())+ interval '-1 day', '1 day'),'yyyy-mm-dd');
SELECT to_char( generate_series( to_timestamp((to_char((now() + interval '-1 month'), 'YYYY-MM-01')), 'YYYY-MM-DD 00:00:00'),    date_trunc('month',now())+ interval '-1 day', '1 day'),'yyyy-mm-dd');

-- 根据当前时间查询当月天数数据 毫秒时间戳需除以1000 转成北京时间
select date_trunc('month',to_timestamp(1680918153389/1000) AT TIME ZONE 'UTC-8');
select date_trunc('month',to_timestamp(1680918153389/1000) AT TIME ZONE 'UTC-8')+ interval '1 month -1 day';

select to_char( generate_series(date_trunc('month',to_timestamp(1680918153389/1000) AT TIME ZONE 'UTC-8'),
 date_trunc('month',to_timestamp(1680918153389/1000) AT TIME ZONE 'UTC-8')+ interval '1 month -1 day', '1 day'),'yyyy-mm-dd') days;

2.PG执行脚本

do
$$
    DECLARE
        unid   varchar;
        newId  varchar;
        date   timestamp;
        icount numeric;
        cur_list CURSOR FOR
            select id
            from f_and_tpm a
            where created_by = 'shw000';
    BEGIN
        date = now();
        OPEN cur_list;
        LOOP
            FETCH cur_list INTO unid;
            EXIT WHEN NOT FOUND;

            /*select count(1)
            into icount
            from f_and_tpm
            where id = unid;
            IF (icount > 1)
            THEN
                CONTINUE ;
            end if;*/

            newId = gen_random_uuid();

            update f_and_tpm
            set id=newId,
                created_time=date,
                --created_by='shw',
                updated_time=date,
                updated_by='shw'
            where id = unid;
            --插入数据
            INSERT INTO public.f_and_tpm_class_correlation (id, created_by, created_time, delete_flag, updated_by,
                                                            updated_time, equipment_id, remark, tpm_class_id, tpm_id)
            VALUES (gen_random_uuid(), 'shw000', date, 0, 'shw000',
                    date, null, null, 'ff8080818726933d0187272f21d902df',
                    newId);

        END LOOP;
        CLOSE cur_list;

    END;
$$

3.分组排序取最新数据

select * from (
select *,row_number() over (partition by code order by created_time desc) rt from f_sys_user) as t
where t.rt=1

4.PG删除多表关联数据(比较少用,记录下以便回忆)

delete from f_qms_process_quality a using  (
SELECT C.* FROM f_prm_product_route A ,f_mdm_factory B ,f_prm_route_step C
         WHERE A.inner_product_no='6D3' AND A.part_type='MC'
           AND A.org_id =B.ID  and b.CODE='DC'
 and c.route_id=a.unid  AND  a.delete_flag=0  AND  b.delete_flag=0   AND  c.delete_flag=0
) t where a.process_id=t.unid;

5.查询当前时间所在第几周

select 1 + date_part('week', dt) - date_part('week', dt - ((date_part('day', dt) - 1) || ' day')::interval)
from (values (now())) data(dt);

PG多表关联更新数据

 update f_weekly_maintenance a set serial_number=t.rn
 from (select id,to_char(week_start_date,'yyyymm')||'0'||row_number() over (partition by months order by weeks ) rn
       from f_weekly_maintenance ) as t
 where t.id= a.id

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PostgreSQL查询执行引擎是负责解释和执行查询计划的组件。查询执行引擎将查询计划按照存储引擎提供的接口访问存储引擎,并返回查询结果。在查询的执行过程中,查询引擎会将查询语句转换为查询树,并进行基于规则的转换和逻辑优化操作,包括对公共表达式、子链接、JOIN/IN/NOT IN等的优化处理。然后,查询引擎根据最优查询访问路径构建查询计划,并生成最终的查询结果。 在PostgreSQL中,查询执行引擎将查询计划转换为中间代码的形式,类似于Java中的字节码。这样一来,无论是PostgreSQL还是其他数据库系统,只要提供对中间代码的访问接口,就可以无缝地执行查询计划。这种设计使得PostgreSQL查询执行引擎更加灵活和可扩展,可以与不同的存储引擎以及现代化的存储系统(如HDFS)进行集成。需要注意的是,为了支持不同的数据库系统或存储引擎,可能需要对源代码进行修改。 综上所述,PostgreSQL查询执行引擎是负责解释和执行查询计划的组件。它通过将查询计划转换为中间代码的形式,并利用最优查询访问路径来生成查询计划,从而实现高效的查询执行。这种设计使得PostgreSQL查询执行引擎可以与不同的存储引擎和数据库系统进行集成。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [PostgreSQL查询引擎源码技术探索--读书笔记](https://blog.csdn.net/junerli/article/details/107139777)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [PostgreSQL查询引擎——select * from where = transform流程](https://blog.csdn.net/asmartkiller/article/details/127414750)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值