一个老是用的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;
相关推荐
ZZHow10243 小时前
JavaWeb开发_Day05
java·笔记·web
CHEN5_023 小时前
【Java虚拟机】垃圾回收机制
java·开发语言·jvm
Warren983 小时前
Lua 脚本在 Redis 中的应用
java·前端·网络·vue.js·redis·junit·lua
xiao-xiang3 小时前
redis-保姆级配置详解
数据库·redis
白鹭5 小时前
MySQL(多表查询练习)
数据库·mysql
艾伦~耶格尔7 小时前
【数据结构进阶】
java·开发语言·数据结构·学习·面试
爪洼传承人7 小时前
18- 网络编程
java·网络编程
smileNicky7 小时前
SpringBoot系列之从繁琐配置到一键启动之旅
java·spring boot·后端
吃掉你也没关系吧7 小时前
【postgresql】一文详解postgresql中的统计模块
sql·postgresql
祈祷苍天赐我java之术7 小时前
Java 迭代器(Iterator)详解
java·开发语言