帮我详细解释下这段代码什么意思with a01 as ( select isname industry, custcode, custname, count(distinct model_code) part_num, ( select count(distinct model_code) from dim_factory_yq ) total_num from dim_factory_yq group by isname, custcode, custname ) select industry, custcode provider_code, custname provider_name, part_num mould_num, round((part_num / total_num) * 100, 2) ratio from a01 order by mould_num desc limit 5
时间: 2024-02-15 12:27:05 浏览: 143
这段代码是一段SQL语句,用于查询工厂模具数量的统计数据,并按模具数量排序输出前五个工厂的相关信息。
具体解释如下:
1. `with a01 as`:这是一个with语句,用于创建一个临时表a01,这个表包含了工厂的行业、客户代码、客户名称、模具数量和总模具数量。
2. `(select isname industry, custcode, custname, count(distinct model_code) part_num, (select count(distinct model_code) from dim_factory_yq) total_num from dim_factory_yq group by isname, custcode, custname)`:这是一个子查询,用于从维度表dim_factory_yq中查询出每个工厂的行业、客户代码、客户名称、模具数量和总模具数量,并按行业、客户代码和客户名称进行分组。
3. `select industry, custcode provider_code, custname provider_name, part_num mould_num, round((part_num / total_num) * 100, 2) ratio from a01 order by mould_num desc limit 5`:这是一个主查询,用于从临时表a01中查询出工厂的行业、客户代码、客户名称、模具数量和模具数量占比,并按模具数量降序排列,最后只输出前五个结果。
具体字段解释如下:
- `industry`:工厂所属行业;
- `provider_code`:工厂客户代码;
- `provider_name`:工厂客户名称;
- `mould_num`:工厂模具数量;
- `ratio`:工厂模具数量占比,保留两位小数。
阅读全文