一个老是用的SQL

1. Tooling Status

1.1 t_get_ready
sql 复制代码
 with
        t_dash as(
        select  npi_product_type,npi_product_type_name  ,npi_product_id ,part_sub_category ,supplier
        from ludp_tooling_dashboard ltd
        where
		npi_product_id is not null
		and npi_product_type = '2f77d2820c64445589b8ac1'
        group by npi_product_type,npi_product_type_name  ,npi_product_id ,part_sub_category ,supplier
        )
        ,project1 as(
        select diu.npi_product_id ,diu.product_name,diu.part_sub_category ,diu.supplier,diu.odm
        from demand_import_use diu
        join t_dash as tem on tem.npi_product_type=diu.product_type  and tem.npi_product_id=diu.npi_product_id and tem.part_sub_category=diu.part_sub_category  and tem.supplier=diu.supplier
        where total_count=1 and value is not null and value::int >0
        and process in(select dict_name  from sys_dict sd where parent_id =6 and description ='1')
        and date >= '2024-11-30'
		and date <= '2024-12-10 23:59:59.999'
        group by diu.npi_product_id ,diu.product_name,diu.part_sub_category ,diu.supplier,diu.odm
        )
       ,t_sensor_day_sql as (
            select product_type,npi_product_id,project_name as product_name,part_sub_category ,supplier,	sensor_day,tooling_id,design_type,odm
            ,mass_production_rate,production_day
            from ludp_dm_sensor_shot_num_day
            where
		production_day > 0
		and npi_product_id is not null
		and sensor_day::date >= '2024-11-30'
		and sensor_day::date <= '2024-12-10 23:59:59.999'
		and product_type = 'PH'
        )
        ,project2 as
        (select npi_product_id ,product_name,part_sub_category,supplier,odm
        from t_sensor_day_sql
        group by npi_product_id ,product_name,part_sub_category,supplier,odm
        )
        ,project3 as
        (select * from  project1
        union
        select * from project2 )
        ,t_record as (
        select
        row_number() over(partition by tooling_id
        order by
        time desc) as idx,state,
        dbr.tooling_id , dbr.sensor_id, dbr.npi_product_id , dbr.parts_sub_category, dbr.supplier
        from
        device_binding_record dbr
        join project3 as td on td.npi_product_id = dbr.npi_product_id and td.part_sub_category = dbr.parts_sub_category and td.supplier = dbr.supplier
        where time <= '2024-12-10 23:59:59.999' 
        )
        ,t_tooling as( -- 1. 总共8条数据
        select
        tooling_id , sensor_id, npi_product_id , parts_sub_category as part_sub_category, supplier
        from t_record
        where idx = 1 and state=1
       and tooling_id  = 'Manaus5G23_Middle Frame_Green_M4'
        )
        ,t_ok as( -- 2. 获取正常数据;
        select
        tem.tooling_id,
        'Production'::varchar tooling_status ,
        '正常'::varchar as process_type
        from t_tooling as tem
        join ludp_dm_sensor_shot_num_day ld on tem.npi_product_id = ld.npi_product_id and ld.tooling_id = tem.tooling_id
        where mass_production_rate> 0   and ld.npi_product_id is not null
        and ld.sensor_day::date ='2024-12-10'
        )
        ,t_sensor_day_temp as( -- ludp_dm_sensor_shot_num_day 日生产表;
        select ld.sensor_day,ld.mass_production_rate,ld.npi_product_id,tooling_id
        from ludp_dm_sensor_shot_num_day ld
        where ld.mass_production_rate <=0   and ld.npi_product_id is not null
        and ld.tooling_id in (select tooling_id from t_tooling)
        and ld.tooling_id not in (select tooling_id from t_ok)
        and ld.sensor_day::date ='2024-12-10'
        )
        ,t_abnormal as(
        select distinct tem.*
        from t_tooling as tem
        left join t_sensor_day_temp ld on  tem.npi_product_id = ld.npi_product_id and ld.tooling_id = tem.tooling_id
        )
        ,t_tpap_row as(-- tpap_abnormal_info 异常表数据
        select
        row_number() over(partition by tai.tooling_id
        order by tai.id desc) as idx,
        sd.description ,
        tai.process_id,tai.tooling_id ,sd.dict_name as tooling_status ,pi3.create_time
        from
        tpap_abnormal_info tai
        left join process_info as pi3 on tai.process_id=pi3.process_id
        left join sys_dict sd on
        sd.dict_code = tai.tooling_status_code
        where
		position( 'PH' in tai.process_id)>0
		and pi3.create_time <= '2024-12-10 23:59:59.999' 
        )
        ,t_tpap_1 as(
        select * from t_tpap_row where idx = 1
        )
        ,t_tpap as(-- tpap_abnormal_info 异常表数据
        select * from t_tpap_1 where  tooling_id  not in (select tooling_id from t_ok)
            and tooling_id in (select tooling_id from t_tooling)
        )
        ,t_normal_1 as(
        select
        'Production'::varchar tooling_status ,
        count(distinct tooling_id)as num,
        '正常'::varchar as process_type
        from t_ok
        )
        ,t_normal_2 as( -- 3. 异常表中正常的数据;
        select
        tai.tooling_status,
        count(1) as num,
        '正常'::varchar as process_type
        from t_tpap as tai
        left join t_abnormal as ta on tai.tooling_id = ta.tooling_id
        where tai.tooling_status is not null
        and tai.tooling_id is not null
        and tai.description = '0'
        group by tai.tooling_status
        )
        ,t_get_ready as(
        select * from t_normal_1
        union
        select * from t_normal_2
        )
        select * from t_get_ready;
相关推荐
疯狂的挖掘机21 分钟前
记一次基于QT的图片操作处理优化思路(包括在图上放大缩小,截图,画线,取值等)
开发语言·数据库·qt
.鸣1 小时前
set和map
java·学习
ha_lydms1 小时前
5、Spark函数_s/t
java·大数据·python·spark·数据处理·maxcompute·spark 函数
奇树谦2 小时前
Qt | 利用map创建多个线程和定时器
网络·数据库·qt
用户47949283569152 小时前
性能提升 4000%!我是如何解决 运营看板 不能跨库&跨库查询慢这个难题的
数据库·后端·postgresql
电商API&Tina2 小时前
跨境电商 API 对接指南:亚马逊 + 速卖通接口调用全流程
大数据·服务器·数据库·python·算法·json·图搜索算法
黄河滴滴2 小时前
java系统变卡变慢的原因是什么?从oom的角度分析
java·开发语言
侠客行03172 小时前
Mybatis二级缓存实现详解
java·mybatis·源码阅读
robinson19882 小时前
验证崖山数据库标量子查询是否带有CACHE功能
数据库·oracle·cache·自定义函数·崖山·标量子查询
老华带你飞2 小时前
农产品销售管理|基于java + vue农产品销售管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端