一个老是用的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;
相关推荐
亲爱的非洲野猪6 分钟前
Oracle与MySQL详细对比
数据库·mysql·oracle
亮1118 分钟前
Maven 编译过程中发生了 Java Heap Space 内存溢出(OutOfMemoryError)
java·开发语言·maven
Matrix708 分钟前
Navicat实现MySQL数据传输与同步完整指南
数据库·mysql
添乱16 分钟前
「Java案例」求PI的值
java
Zhu_S W18 分钟前
深入理解Java虚拟机:Java内存区域与内存溢出异常
java·开发语言·jvm
快乐非自愿20 分钟前
商品中心—库存分桶高并发的优化文档
java·前端·spring
鸡蛋灌Bean40 分钟前
Java常用设计模式大全
java·开发语言·设计模式
喝可乐的布偶猫41 分钟前
Java-----韩顺平单例设计模式学习笔记
java·笔记·设计模式
AskHarries1 小时前
深入探索Java虚拟机的神秘接口:JVMTI
java·jvm
Z字小熊饼干爱吃保安1 小时前
面试技术问题总结一
数据库·面试·职场和发展