(select date(a.claim_time) as date, a.lot_id, a.lot_type,a.mainpd_id,a.prodspec_id,a.custprod_id, case when(date(b.created_time) <= '2009-01-05') then b.created_time + 21 days else b.created_time end as created_time, CASE WHEN A.CUST_id in ('MCA','NPA','SET') THEN a.COMPLETE_TIME ELSE a.COMPLETE_TIME END COMPLETE_TIME, a.cust_id as customer_id, a.ope_category, c.layer From f3rpt.F3_TB_DAILY_FABOUT a, f3rpt.fvlot b, (select mainpd_id, sum(masks)layer from f3rpt.ASMCRPT_VW_MAINPD_MASKS_ALL group by mainpd_id) as c, (select * from (select lot_id, max(claim_time)claim_time, count(case when(ope_category='Ship')then lot_id else null end) as LS, count(case when(ope_category='Unship') then lot_id else null end) as LUS from f3rpt.F3_TB_DAILY_FABOUT where substr(lot_id,1,2) not in('CA','CW','ES','E0','EM') and lot_type = 'Production' AND LOT_ID NOT LIKE 'H%' and substr(lot_id,7,4)='.00F' and ope_category in ('Ship','Unship') and year(claim_time) = year(current date - 1 days) and month(claim_time) = month(current date - 1 days) group by lot_id) as a where LS - LUS > 0 ) as lot Where a.lot_id = b.lot_id and b.mainpd_id = c.mainpd_id and a.lot_id = lot.LOT_ID and a.claim_time = lot.claim_time and a.ope_category = 'Ship' and a.cust_id in ('SM','BOE','GSC','NPA','GTA') ) as lot_hs 以上嵌套子查询,请拆分下
时间: 2024-02-14 09:12:01 浏览: 145
面的嵌套子查询,以便更好地理解和优化该查询。
首先,最外层的查询选择了一些列并从其他查询中选择了一些数据,其中最重要的是日期、批次ID、批次类型、主产品ID、产品规格ID、客户产品ID、创建时间、完成时间、客户ID、操作类别和层。这些信息都是从其他查询中获取的。
接下来,我们来看第一个子查询:
```
select mainpd_id, sum(masks)layer from f3rpt.ASMCRPT_VW_MAINPD_MASKS_ALL group by mainpd_id
```
该子查询从 `f3rpt.ASMCRPT_VW_MAINPD_MASKS_ALL` 表中获取主产品ID和层信息,并将其按主产品ID分组。
接下来是第二个子查询:
```
select lot_id, max(claim_time)claim_time, count(case when(ope_category='Ship')then lot_id else null end) as LS, count(case when(ope_category='Unship') then lot_id else null end) as LUS
from f3rpt.F3_TB_DAILY_FABOUT
where substr(lot_id,1,2) not in('CA','CW','ES','E0','EM')
and lot_type = 'Production' AND LOT_ID NOT LIKE 'H%'
and substr(lot_id,7,4)='.00F'
and ope_category in ('Ship','Unship')
and year(claim_time) = year(current date - 1 days)
and month(claim_time) = month(current date - 1 days)
group by lot_id
```
该子查询从 `f3rpt.F3_TB_DAILY_FABOUT` 表中获取批次ID、要求时间、已发运批次数和未发运批次数,并将其按批次ID分组。该子查询还应用了一些过滤条件,例如要求时间在特定日期之前、批次类型为生产、批次ID不以特定字符串开头等。
最后,最外层的查询将这些子查询组合在一起,并应用了一些其他过滤条件,例如操作类别为发运、客户ID为特定值等。
拆分这个查询的好处是,可以更好地理解每个子查询的作用和结果,并且可以更容易地对查询进行优化,例如通过添加索引来提高性能。
阅读全文