请以最详细的方式解释这段代码with a0 as ( select case when prodlinename in ('冷柜', '冰箱') then '制冷' when prodlinename in ('商空') then '楼宇' when prodlinename in ('家空') then '空调' else prodlinename end industry, mouldno, cast(avgintervals as double) as avgintervals, pt from dl_yf_seq.tt_hmzy_haier_mould_iot_fenqu where avgintervals is not null and avgintervals not in('', '0', '0.0') and prodlinename in('冷柜', '冰箱', '商空', '家空', '厨电', '洗涤', '热水器') and substr(pt, 1, 4) = from_unixtime(unix_timestamp(), 'yyyy') ), a1 as ( select model_code, cast(ratedcycle as double) as ratedcycle from dh_yf.dim_udo_model_dim_asset where ratedCycle is not null and ratedCycle not in('', '/', '0', '0.0') ), a2 as ( select industry, mouldno, avgintervals, b.ratedcycle, case when avgintervals<= ratedCycle then 1 else 0 end db, pt from a0 a left join a1 b on a.mouldno = b.model_code ), a3 as ( select pt, industry, count(mouldno) as kj_module_num, sum(db) as db_module_num from a2 group by pt, industry ) insert OVERWRITE table dh_yf.tg_dim_udo_model_scxldb select pt, industry, cast(kj_module_num as string) kj_module_num, cast(db_module_num as string) db_module_num, from_unixtime(unix_timestamp(),'yyyyMMdd') as dh_etl_date from a3
时间: 2024-02-14 17:13:16 浏览: 90
这段代码使用了 SQL 语言,主要包含了四个子查询,最终将结果插入到一个名为 dh_yf.tg_dim_udo_model_scxldb 的表中。
1. 第一个子查询
第一个子查询使用了 WITH 子句,创建了一个名为 a0 的临时表,其作用是将 tt_hmzy_haier_mould_iot_fenqu 表中的数据进行筛选和转换。具体操作如下:
```
with a0 as (
select
case
when prodlinename in ('冷柜', '冰箱') then '制冷'
when prodlinename in ('商空') then '楼宇'
when prodlinename in ('家空') then '空调'
else prodlinename
end industry,
mouldno,
cast(avgintervals as double) as avgintervals,
pt
from
dl_yf_seq.tt_hmzy_haier_mould_iot_fenqu
where
avgintervals is not null
and avgintervals not in('', '0', '0.0')
and prodlinename in('冷柜', '冰箱', '商空', '家空', '厨电', '洗涤', '热水器')
and substr(pt, 1, 4) = from_unixtime(unix_timestamp(), 'yyyy')
)
```
- 首先使用 CASE WHEN 语句对 prodlinename 进行分类转换,将其转换为行业类型。
- 然后将 avgintervals 字段转换为 double 类型。
- 最后筛选出符合条件的数据,其中 substr(pt, 1, 4) = from_unixtime(unix_timestamp(), 'yyyy') 表示只选择当年的数据,pt 表示时间戳。
2. 第二个子查询
第二个子查询创建了一个名为 a1 的临时表,其作用是从 dim_udo_model_dim_asset 表中筛选出 ratedCycle 不为空的数据,并将其转换为 double 类型。
```
with a1 as (
select
model_code,
cast(ratedcycle as double) as ratedcycle
from
dh_yf.dim_udo_model_dim_asset
where
ratedCycle is not null
and ratedCycle not in('', '/', '0', '0.0')
)
```
3. 第三个子查询
第三个子查询将 a0 和 a1 两个临时表进行了关联,计算出每个模具的 db 值。
```
with a2 as (
select
industry,
mouldno,
avgintervals,
b.ratedcycle,
case
when avgintervals<= ratedCycle then 1
else 0
end db,
pt
from
a0 a
left join a1 b on a.mouldno = b.model_code
)
```
- 首先对 a0 和 a1 两个临时表进行了左连接。
- 然后使用 CASE WHEN 语句计算出每个模具的 db 值,如果 avgintervals <= ratedCycle,则 db 值为 1,否则为 0。
4. 第四个子查询
第四个子查询将 a2 表按照时间和行业进行分组,计算出每个行业的 kj_module_num 和 db_module_num 值,并将结果插入到 dh_yf.tg_dim_udo_model_scxldb 表中。
```
with a3 as (
select
pt,
industry,
count(mouldno) as kj_module_num,
sum(db) as db_module_num
from
a2
group by
pt,
industry
)
insert OVERWRITE table dh_yf.tg_dim_udo_model_scxldb
select
pt,
industry,
cast(kj_module_num as string) kj_module_num,
cast(db_module_num as string) db_module_num,
from_unixtime(unix_timestamp(),'yyyyMMdd') as dh_etl_date
from
a3
```
- 首先将 a2 表按照时间和行业进行分组,计算出每个行业的 kj_module_num 和 db_module_num 值。
- 然后将结果插入到 dh_yf.tg_dim_udo_model_scxldb 表中,其中 kj_module_num 和 db_module_num 被转换为 string 类型,dh_etl_date 表示当前日期。
阅读全文