You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
2.3 KiB
Markdown

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

```mysql
CREATE VIEW `mes_view_report_plan_detail` AS SELECT
`wp`.`id` AS `id`,
`wp`.`report_id` AS `report_id`,
`wp`.`plan_id` AS `plan_id`,
`wp`.`product_id` AS `product_id`,
`wp`.`quality_number_plan` AS `quality_number_plan`,
`wp`.`waste_number_plan` AS `waste_number_plan`,
`pd`.`user_id` AS `user_id`,
`pd`.`org_type` AS `org_type`,
`pd`.`org_id` AS `org_id`,
`pd`.`report_date` AS `report_date`,
FROM
(
`mes_work_report_plan` `wp`
JOIN `mes_produce_report_detail` `pd` ON ((
`wp`.`report_id` = `pd`.`id`
)))
```
存在视图,显示工人生产报工产品数量分配给计划的详细情况,
report_id是报工单idplan_id是计划idproduct_id是产品idquality_number_plan是分配给计划的合格产品数量
waste_number_plan是分配给计划的不合格产品数量user_id是工人idorg_type是工序类别report_date是报工日期
现在请你编写mysql的sql语句基于这个视图创建另一个视图汇总同一个计划同一个产品同一个工序的合格产品数和不合格产品数
要基于现有的mes_view_report_plan_detail视图创建另一个视图汇总同一个计划、同一个产品和同一个工序的合格产品数和不合格产品数你可以使用MySQL的GROUP BY和聚合函数来实现。以下是编写的SQL语句示例
```mysql
CREATE VIEW `mes_view_summary_report` AS
SELECT
`plan_id`,
`product_id`,
`org_type`,
SUM(`quality_number_plan`) AS `total_quality_number`,
SUM(`waste_number_plan`) AS `total_waste_number`
FROM
`mes_view_report_plan_detail`
GROUP BY
`plan_id`, `product_id`, `org_type`;
```
在这个SQL语句中
创建了一个名为mes_view_summary_report的新视图。
从现有的mes_view_report_plan_detail视图中选择plan_id、product_id、org_type以及分配给计划的合格产品数量和不合格产品数量字段。
使用SUM()函数对合格产品数量和不合格产品数量进行求和,得到同一个计划、同一个产品和同一个工序的总合格产品数和不合格产品数。
最后使用GROUP BY子句按plan_id、product_id和org_type分组确保针对相同计划、产品和工序的记录被聚合在一起。
通过这个SQL语句你可以创建一个新的视图mes_view_summary_report用于汇总同一个计划、同一个产品和同一个工序的合格产品数和不合格产品数的信息