活动介绍

@Query(value = "select COALESCE(MAX(CAST(p.productid AS INTEGER)),0) from product_information p where p.servicetype = :servicetype", nativeQuery = true) Integer getMaxProductidByServicetype(@Param("servicetype") String servicetype);MySQL报错

时间: 2025-07-20 13:17:40 浏览: 12
<think>我们正在处理一个在MySQL环境下运行的Spring Data JPA的@Query注解报错问题。查询语句是: ```sql select COALESCE(MAX(CAST(p.productid AS INTEGER)),0) from product_information p where p.servicetype = :servicetype ``` 在MySQL中,这个查询可能会因为多种原因报错,比如`productid`列包含非数字值,或者值超出了整数的范围等。 ### 常见错误原因及解决方案: 1. **非数字值问题**:如果`productid`列包含非数字字符(比如字母、符号等),则`CAST`函数会失败。 2. **整数范围问题**:`INTEGER`类型在MySQL中是一个有符号整数,范围是-2147483648到2147483647。如果`productid`中的数字超出这个范围,转换会失败。 3. **空字符串或NULL**:空字符串`''`无法转换为整数,但注意`COALESCE`函数会处理NULL值,但不会处理转换失败的情况。 ### 解决方案: #### 方案1:使用正则表达式过滤非数字值(推荐) 修改查询,只尝试转换那些完全由数字组成的字符串。在MySQL中,我们可以使用`REGEXP`来过滤。 ```java @Query(value = "SELECT COALESCE(MAX(CAST(p.productid AS UNSIGNED)), 0) " + "FROM product_information p " + "WHERE p.servicetype = :servicetype " + "AND p.productid REGEXP '^[0-9]+$'", nativeQuery = true) Integer getMaxProductidByServicetype(@Param("servicetype") String servicetype); ``` **解释**: - `CAST(p.productid AS UNSIGNED)`:将字符串转换为无符号整数(可以避免负数,并且范围更大:0到4294967295)。 - `p.productid REGEXP '^[0-9]+$'`:确保只处理由纯数字组成的字符串。 #### 方案2:使用更大的整数类型 如果数字可能超过无符号整数的范围(4294967295),我们可以使用`BIGINT`类型。 ```java @Query(value = "SELECT COALESCE(MAX(CAST(p.productid AS UNSIGNED BIGINT)), 0) " + "FROM product_information p " + "WHERE p.servicetype = :servicetype " + "AND p.productid REGEXP '^[0-9]+$'", nativeQuery = true) Long getMaxProductidByServicetype(@Param("servicetype") String servicetype); ``` 注意:这里返回类型改为了`Long`,因为`BIGINT`可能超出`Integer`的范围。 #### 方案3:使用MySQL的`CONVERT`函数 `CONVERT`函数也可以用于转换,但和`CAST`类似。所以主要问题还是在于数据过滤。 #### 方案4:处理空字符串 如果存在空字符串,我们可以将其过滤掉: ```java @Query(value = "SELECT COALESCE(MAX(CAST(p.productid AS UNSIGNED)), 0) " + "FROM product_information p " + "WHERE p.servicetype = :servicetype " + "AND p.productid != '' " + // 排除空字符串 "AND p.productid REGEXP '^[0-9]+$'", nativeQuery = true) Integer getMaxProductidByServicetype(@Param("servicetype") String servicetype); ``` ### 错误排查 如果上述方法仍然报错,我们可以通过以下步骤排查: 1. **查询出非数字的数据**:在数据库执行以下查询,找出非数字的数据,以便清理或处理。 ```sql SELECT productid FROM product_information WHERE servicetype = 'your_service_type' AND (productid = '' OR productid NOT REGEXP '^[0-9]+$'); ``` 2. **测试转换**:测试转换是否成功。 ```sql SELECT productid, CAST(productid AS UNSIGNED) FROM product_information WHERE servicetype = 'your_service_type' AND productid REGEXP '^[0-9]+$'; ``` ### 最佳实践 1. **数据校验**:在应用层或数据库层确保`productid`只存储数字。 2. **修改数据类型**:如果可能,将`productid`的数据类型改为整数类型,避免使用字符串存储数字。 3. **异常处理**:在Repository方法中捕获异常,并返回默认值。 ### 异常处理示例 ```java public interface ProductRepository extends JpaRepository<ProductInformation, Long> { // 尝试使用上面的安全查询 // 如果仍然可能出错,可以使用默认方法处理异常 default Integer getMaxProductidByServicetypeSafe(String servicetype) { try { return getMaxProductidByServicetype(servicetype); } catch (Exception e) { // 记录异常 return 0; } } } ``` ### 总结 在MySQL中,使用`CAST`函数转换字符串为整数时,必须确保字符串是有效的数字,并且不超出范围。通过添加正则表达式过滤,我们可以避免转换失败。同时,考虑使用更大范围的整数类型(如`BIGINT`)来避免溢出。 §§相关问题§§: 1. 在PostgreSQL中,如何安全地将字符串转换为整数? 2. 使用Spring Data JPA时,如何优雅地处理原生SQL查询的异常? 3. 在数据库设计中,如何避免在应该存储数字的字段中存储非数字字符? 4. 如何优化查询以获取某个分类下的最大数值(例如productid)的性能?</think>### 解决 MySQL 中 `CAST` 报错问题:完整解决方案 在 MySQL 环境下执行以下查询时出现错误: ```java @Query(value = "select COALESCE(MAX(CAST(p.productid AS INTEGER)),0) " + "from product_information p " + "where p.servicetype = :servicetype", nativeQuery = true) Integer getMaxProductidByServicetype(@Param("servicetype") String servicetype); ``` 以下是针对 MySQL 的详细解决方案: --- ### 错误原因分析(MySQL 特有) 1. **`CAST AS INTEGER` 语法问题**: MySQL 不支持 `CAST AS INTEGER`,应使用 `CAST AS UNSIGNED` 或 `CAST AS SIGNED` 2. **非数字字符问题**: MySQL 遇到非数字字符时会返回截断警告或错误 3. **数值范围问题**: `INTEGER` 范围有限(-2147483648 到 2147483647),大数值需使用 `BIGINT` 4. **空字符串问题**: MySQL 将空字符串 `''` 转换为 0,可能导致逻辑错误 --- ### 解决方案代码(MySQL 优化版) #### 方案 1:安全转换 + 正则过滤(推荐) ```java @Query(value = "SELECT COALESCE(MAX(CAST(p.productid AS UNSIGNED)), 0) " + "FROM product_information p " + "WHERE p.servicetype = :servicetype " + "AND p.productid REGEXP '^[0-9]+$'", nativeQuery = true) Long getMaxProductidByServicetype(@Param("servicetype") String servicetype); ``` **改进说明**: 1. 使用 `CAST AS UNSIGNED` 替代 `CAST AS INTEGER` 2. 添加 `REGEXP '^[0-9]+$'` 过滤非数字值 3. 返回类型改为 `Long` 避免溢出 4. 使用 `UNSIGNED` 处理大数值(最大 18446744073709551615) #### 方案 2:使用 MySQL 8.0+ 的 `TRY_CAST` ```java @Query(value = "SELECT COALESCE(MAX(TRY_CAST(p.productid AS UNSIGNED)), 0) " + "FROM product_information p " + "WHERE p.servicetype = :servicetype", nativeQuery = true) Long getMaxProductidSafe(@Param("servicetype") String servicetype); ``` **注意**:`TRY_CAST` 仅在 MySQL 8.0.13+ 版本可用 #### 方案 3:处理超大数值(超过 BIGINT UNSIGNED) ```java @Query(value = "SELECT COALESCE(MAX(" + " CASE " + " WHEN p.productid REGEXP '^[0-9]{1,20}$' " + " THEN CAST(p.productid AS DECIMAL(65,0)) " + " ELSE NULL " + " END), 0) " + "FROM product_information p " + "WHERE p.servicetype = :servicetype", nativeQuery = true) BigInteger getMaxProductidLarge(@Param("servicetype") String servicetype); ``` **说明**: 1. 使用 `DECIMAL(65,0)` 处理超大整数(最大 65 位) 2. 返回类型改为 `BigInteger` 3. 添加长度检查 `{1,20}` 避免无效值 --- ### 错误排查工具(MySQL 专用) ```java @Service public class MySQLProductIdValidator { private final JdbcTemplate jdbcTemplate; public MySQLProductIdValidator(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } /** * 查找导致转换失败的无效 productid */ public List<String> findInvalidProductIds(String serviceType) { String sql = "SELECT productid " + "FROM product_information " + "WHERE servicetype = ? " + "AND (productid = '' " + " OR productid IS NULL " + " OR productid NOT REGEXP '^[0-9]+$')"; return jdbcTemplate.queryForList(sql, String.class, serviceType); } /** * 测试转换是否成功 */ public boolean testConversion(String serviceType) { try { String testSql = "SELECT CAST(productid AS UNSIGNED) " + "FROM product_information " + "WHERE servicetype = ? " + "LIMIT 1"; jdbcTemplate.queryForObject(testSql, Long.class, serviceType); return true; } catch (DataAccessException e) { return false; } } /** * 获取实际最大值(安全方法) */ public Long getActualMaxProductId(String serviceType) { String sql = "SELECT productid " + "FROM product_information " + "WHERE servicetype = ? " + "AND productid REGEXP '^[0-9]+$' " + "ORDER BY LENGTH(productid) DESC, productid DESC " + "LIMIT 1"; try { String maxIdStr = jdbcTemplate.queryForObject(sql, String.class, serviceType); return Long.parseLong(maxIdStr); } catch (EmptyResultDataAccessException e) { return 0L; } } } ``` --- ### MySQL 性能优化方案 #### 添加虚拟列 + 索引(MySQL 5.7+) ```sql ALTER TABLE product_information ADD productid_numeric BIGINT UNSIGNED GENERATED ALWAYS AS ( CASE WHEN productid REGEXP '^[0-9]+$' THEN CAST(productid AS UNSIGNED) ELSE NULL END ) VIRTUAL, ADD INDEX idx_productid_numeric (productid_numeric); ``` #### Java 查询使用虚拟列 ```java @Query(value = "SELECT COALESCE(MAX(productid_numeric), 0) " + "FROM product_information p " + "WHERE p.servicetype = :servicetype", nativeQuery = true) Long getMaxProductidOptimized(@Param("servicetype") String servicetype); ``` **优势**: 1. 避免每次转换的计算开销 2. 可以使用索引加速查询 3. 确保只处理有效数字 --- ### 完整解决方案(Spring Boot + MySQL) ```java // 实体类 @Entity @Table(name = "product_information") public class ProductInformation { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "productid") @Pattern(regexp = "^\\d+$", message = "Product ID must be numeric") private String productId; @Column(name = "servicetype") private String serviceType; // Getters and setters } // Repository public interface ProductRepository extends JpaRepository<ProductInformation, Long> { // 安全查询方法 @Query(nativeQuery = true, value = """ SELECT COALESCE(MAX(CAST(p.productid AS UNSIGNED)), 0) FROM product_information p WHERE p.servicetype = :servicetype AND p.productid REGEXP '^[0-9]+$' """) Long getMaxProductIdSafe(@Param("servicetype") String serviceType); // 备选方法:处理超大数值 @Query(nativeQuery = true, value = """ SELECT MAX(CAST(productid AS DECIMAL(65, 0))) FROM product_information WHERE servicetype = :servicetype AND productid REGEXP '^[0-9]{1,65}$' """) Optional<BigDecimal> getMaxProductIdLarge(@Param("servicetype") String serviceType); default Long getMaxProductId(String serviceType) { return getMaxProductIdSafe(serviceType); } } // 服务层 @Service public class ProductService { private final ProductRepository productRepository; public ProductService(ProductRepository productRepository) { this.productRepository = productRepository; } public Long getMaxProductId(String serviceType) { try { return productRepository.getMaxProductId(serviceType); } catch (DataAccessException ex) { // 处理转换错误 return productRepository.findTopByServiceTypeOrderByProductIdDesc(serviceType) .map(ProductInformation::getProductId) .filter(id -> id.matches("\\d+")) .map(Long::parseLong) .orElse(0L); } } } ``` --- ### MySQL 错误处理建议 ```java @RestControllerAdvice public class MySQLExceptionHandler { private static final Logger logger = LoggerFactory.getLogger(MySQLExceptionHandler.class); @ExceptionHandler(DataAccessException.class) public ResponseEntity<ErrorResponse> handleMySQLException(DataAccessException ex) { String errorCode = "DATABASE_ERROR"; String message = "数据库操作失败"; // 解析MySQL错误代码 if (ex.getCause() instanceof SQLException) { SQLException sqlEx = (SQLException) ex.getCause(); // MySQL 错误代码 1292: 截断错误 if (sqlEx.getErrorCode() == 1292) { errorCode = "INVALID_NUMERIC_CONVERSION"; message = "数字转换错误:请检查productid格式"; logger.warn("数字转换失败: {}", sqlEx.getMessage()); } // MySQL 错误代码 1264: 超出范围 else if (sqlEx.getErrorCode() == 1264) { errorCode = "NUMERIC_OVERFLOW"; message = "数值超出范围:请使用更大的数据类型"; } } return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(new ErrorResponse(errorCode, message)); } } ```
阅读全文

相关推荐

(SELECT n1.ItemCode AS 货号, COALESCE(n.NumAtCard, n1.U_PurContNo) AS 采购合同号, SUM(n1.Quantity) AS 到货数量, STRING_AGG(CAST(n.DocEntry AS VARCHAR(20)), ', ') WITHIN GROUP ( ORDER BY n.DocEntry ) AS 到货单据号 FROM SINO_SAP.dbo.OPDN n LEFT JOIN SINO_SAP.dbo.PDN1 n1 ON n.DocEntry = n1.DocEntry LEFT JOIN SINO_SAP.dbo.OUDP p ON n1.U_Department = p.Name WHERE n.CANCELED = 'n' AND p.Remarks = '经销体系' AND n1.U_SaleContNo = '备货' AND COALESCE(n.NumAtCard, n1.U_PurContNo) IS NOT NULL and n.NumAtCard not like '%CFBZ%' --拆分包装 and n.U_UpTransNo is null --非调拨 -- and COALESCE(n.NumAtCard, n1.U_PurContNo) = '2019ZYHT86021' GROUP BY COALESCE(n.NumAtCard, n1.U_PurContNo), n1.ItemCode) t1 ( SELECT n1.ItemCode AS 货号, COALESCE(n.NumAtCard, n1.U_PurContNo) AS 采购合同号, SUM(n1.Quantity) AS 退货数量, STRING_AGG(CAST(n.DocEntry AS VARCHAR(20)), ', ') WITHIN GROUP ( ORDER BY n.DocEntry ) AS 到货单据号 FROM SINO_SAP.dbo.ORPD n LEFT JOIN SINO_SAP.dbo.RPD1 n1 ON n.DocEntry = n1.DocEntry LEFT JOIN SINO_SAP.dbo.OUDP p ON n1.U_Department = p.Name WHERE n.CANCELED = 'n' AND p.Remarks = '经销体系' AND n1.U_SaleContNo = '备货' AND COALESCE(n.NumAtCard, n1.U_PurContNo) IS NOT NULL and n.NumAtCard not like '%CFBZ%' --拆分包装 and n.U_UpTransNo is null --非调拨 -- and COALESCE(n.NumAtCard, n1.U_PurContNo) = '2019ZYHT86021' GROUP BY COALESCE(n.NumAtCard, n1.U_PurContNo), n1.ItemCode) t2 ( SELECT n1.ItemCode AS 货号, COALESCE(n.NumAtCard, n1.U_PurContNo) AS 采购合同号, SUM(n1.Quantity) AS 贷项数量, STRING_AGG(CAST(n.DocEntry AS VARCHAR(20)), ', ') WITHIN GROUP ( ORDER BY n.DocEntry ) AS 到货单据号 FROM SINO_SAP.dbo.ORPC n LEFT JOIN SINO_SAP.dbo.RPC1 n1 ON n.DocEntry = n1.DocEntry LEFT JOIN SINO_SAP.dbo.OUDP p ON n1.U_Department = p.Name WHERE n.CANCELED = 'n' AND p.Remarks = '经销体系' AND n1.U_SaleContNo = '备货' AND COALESCE(n.NumAtCard, n1.U_PurContNo) IS NOT NULL and n.NumAtCard not like '%CFBZ%' --拆分包装 and n.U_UpTransNo is null --非调拨 -- and COALESCE(n.NumAtCard, n1.U_PurContNo) = '2019ZYHT86021' GROUP BY COALESCE(n.NumAtCard, n1.U_PurContNo), n1.ItemCode) t3 将t1 t2 t3 用union all 合并

WITH -- 1. 计算订单数量 (按采购合同号+物料分组) order_qty AS ( SELECT COALESCE(n.NumAtCard, n1.U_PurContNo) AS 采购合同号, n1.ItemCode AS 物料编码, MIN(n.DocDate) AS 最早订单日期, -- 用于按时间顺序分配在途 SUM(n1.Quantity) AS 订单数量, STRING_AGG(CAST(n.DocEntry AS VARCHAR(20)), ', ') WITHIN GROUP (ORDER BY n.DocDate) AS 采购单据号 FROM SINO_SAP.dbo.ORDR n JOIN SINO_SAP.dbo.RDR1 n1 ON n.DocEntry = n1.DocEntry JOIN SINO_SAP.dbo.OUDP p ON n1.U_Department = p.Name WHERE n1.LineStatus = 'o' AND p.Remarks = '经销体系' AND n.DocDate >= '2024-01-01' AND COALESCE(n.NumAtCard, n1.U_PurContNo) IS NOT NULL GROUP BY COALESCE(n.NumAtCard, n1.U_PurContNo), n1.ItemCode ), -- 2. 计算占货数量 (已分配库存) alloc_qty AS ( SELECT COALESCE(T0.NumAtCard, T1.U_PurContNo) AS 采购合同号, J1.itemcode AS 物料编码, SUM(J1.ALLocqty) AS 占货数量 FROM SINO_SAP.dbo.ORDR T0 JOIN SINO_SAP.dbo.RDR1 T1 ON T0.DocEntry = T1.DocEntry JOIN SINO_SAP.dbo.OITL J0 ON J0.DocEntry = T1.DocEntry AND J0.DocType = '17' AND J0.DocLine = T1.LineNum JOIN SINO_SAP.dbo.ITL1 J1 ON J1.LogEntry = J0.LogEntry WHERE T1.LineStatus = 'o' GROUP BY COALESCE(T0.NumAtCard, T1.U_PurContNo), J1.itemcode ), -- 3. 计算在途总量 (按物料分组) in_transit_total AS ( SELECT ItemCode AS 物料编码, SUM(未到货数量) AS 在途总量 FROM ( -- 报价单数量 SELECT n1.ItemCode, SUM(n1.Quantity) AS 未到货数量 FROM SINO_SAP.dbo.OPQT n JOIN SINO_SAP.dbo.PQT1 n1 ON n.DocEntry = n1.DocEntry JOIN SINO_SAP.dbo.OUDP p ON n1.U_Department = p.Name WHERE n.CANCELED = 'n' AND p.Remarks = '经销体系' AND n1.U_SaleContNo = '备货' AND COALESCE(n.NumAtCard, n1.U_PurContNo) IS NOT NULL AND n.DocDate >= '2024-01-01' GROUP BY n1.ItemCode UNION ALL -- 减去实际到货(正向) SELECT n1.ItemCode, -SUM(n1.Quantity) AS 未到货数量 FROM SINO_SAP.dbo.OPDN n JOIN SINO_SAP.dbo.PDN1 n1 ON n.DocEntry = n1.DocEntry JOIN SINO_SAP.dbo.OUDP p ON n1.U_Department = p.Name WHERE n.CANCELED = 'n' AND p.Remarks = '经销体系' AND n1.U_SaleContNo = '备货' AND COALESCE(n.NumAtCard, n1.U_PurContNo) IS NOT NULL AND n.NumAtCard NOT LIKE '%CFBZ%' AND n.U_UpTransNo IS NULL GROUP BY n1.ItemCode UNION ALL -- 减去退货(负向) SELECT n1.ItemCode, -SUM(n1.Quantity) AS 未到货数量 FROM SINO_SAP.dbo.ORPD n JOIN SINO_SAP.dbo.RPD1 n1 ON n.DocEntry = n1.DocEntry JOIN SINO_SAP.dbo.OUDP p ON n1.U_Department = p.Name WHERE n.CANCELED = 'n' AND p.Remarks = '经销体系' AND n1.U_SaleContNo = '备货' AND COALESCE(n.NumAtCard, n1.U_PurContNo) IS NOT NULL AND n.NumAtCard NOT LIKE '%CFBZ%' AND n.U_UpTransNo IS NULL GROUP BY n1.ItemCode UNION ALL -- 减去贷项(负向) SELECT n1.ItemCode, -SUM(n1.Quantity) AS 未到货数量 FROM SINO_SAP.dbo.ORPC n JOIN SINO_SAP.dbo.RPC1 n1 ON n.DocEntry = n1.DocEntry JOIN SINO_SAP.dbo.OUDP p ON n1.U_Department = p.Name WHERE n.CANCELED = 'n' AND p.Remarks = '经销体系' AND n1.U_SaleContNo = '备货' AND COALESCE(n.NumAtCard, n1.U_PurContNo) IS NOT NULL AND n.NumAtCard NOT LIKE '%CFBZ%' AND n.U_UpTransNo IS NULL GROUP BY n1.ItemCode ) t GROUP BY ItemCode HAVING SUM(未到货数量) > 0 -- 仅保留正数在途 ), -- 4. 计算现缺数量 (订单量 - 占货量) shortage AS ( SELECT o.采购合同号, o.物料编码, o.最早订单日期, o.订单数量, o.采购单据号, COALESCE(a.占货数量, 0) AS 占货数量, GREATEST(o.订单数量 - COALESCE(a.占货数量, 0), 0) AS 现缺数量, -- 计算累计占货比例 (用于分析) COALESCE(a.占货数量, 0) * 1.0 / NULLIF(o.订单数量, 0) AS 占货率 FROM order_qty o LEFT JOIN alloc_qty a ON o.采购合同号 = a.采购合同号 AND o.物料编码 = a.物料编码 ), -- 5. 分配在途数量 (按时间顺序) alloc_in_transit AS ( SELECT s.*, it.在途总量, -- 按时间顺序分配在途 (FIFO) CASE WHEN SUM(s.现缺数量) OVER ( PARTITION BY s.物料编码 ORDER BY s.最早订单日期, s.采购合同号 ) <= COALESCE(it.在途总量, 0) THEN s.现缺数量 WHEN COALESCE(it.在途总量, 0) > SUM(s.现缺数量) OVER ( PARTITION BY s.物料编码 ORDER BY s.最早订单日期, s.采购合同号 ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ) THEN COALESCE(it.在途总量, 0) - SUM(s.现缺数量) OVER ( PARTITION BY s.物料编码 ORDER BY s.最早订单日期, s.采购合同号 ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ) ELSE 0 END AS 分配的在途数量 FROM shortage s LEFT JOIN in_transit_total it ON s.物料编码 = it.物料编码 ) -- 最终结果: 计算需下单数量 SELECT 采购合同号 AS 售订单合同号, 物料编码, 最早订单日期, 订单数量, 采购单据号 AS 售订单单据号, 占货数量, 占货率, 现缺数量, COALESCE(在途总量, 0) AS 总在途数量, 分配的在途数量, GREATEST(现缺数量 - 分配的在途数量, 0) AS 需下单数量 FROM alloc_in_transit ORDER BY 物料编码, 最早订单日期, 采购合同号; 因为是sqlserver代码 所以 greatest不可用

– CREATE PROCEDURE dbo.CalculatePurchaseRequirements – AS – BEGIN – SET NOCOUNT ON; -- 1. 计算订单数量 (按采购合同号+物料分组) WITH order_qty AS ( SELECT COALESCE(n.NumAtCard, n1.U_PurContNo) AS 采购合同号, n1.ItemCode AS 物料编码, MIN(n.DocDate) AS 最早订单日期, SUM(n1.Quantity) AS 订单数量, STRING_AGG(CAST(n.DocEntry AS VARCHAR(20)), ', ') WITHIN GROUP (ORDER BY n.DocDate) AS 采购单据号 FROM SINO_SAP.dbo.ORDR n left JOIN SINO_SAP.dbo.RDR1 n1 ON n.DocEntry = n1.DocEntry left JOIN SINO_SAP.dbo.OUDP p ON n1.U_Department = p.Name left join [ZY_VIEW_XM] xm on (n.NumAtCard = xm.xm_yn or n1.U_PurContNo = xm.xm_yn) WHERE n1.LineStatus = 'o' AND p.Remarks = '经销体系' AND n.DocDate >= '2024-01-01' AND COALESCE(n.NumAtCard, n1.U_PurContNo) IS NOT NULL and n.cardcode not in ('C005596','C003433') and xm.xm_yn is null GROUP BY COALESCE(n.NumAtCard, n1.U_PurContNo), n1.ItemCode ), -- 2. 计算占货数量 alloc_qty AS ( SELECT COALESCE(T0.NumAtCard, T1.U_PurContNo) AS 采购合同号, J1.itemcode AS 物料编码, SUM(J1.ALLocqty) AS 占货数量 FROM SINO_SAP.dbo.ORDR T0 left JOIN SINO_SAP.dbo.RDR1 T1 ON T0.DocEntry = T1.DocEntry left JOIN SINO_SAP.dbo.OITL J0 ON J0.DocEntry = T1.DocEntry AND J0.DocType = '17' AND J0.DocLine = T1.LineNum JOIN SINO_SAP.dbo.ITL1 J1 ON J1.LogEntry = J0.LogEntry WHERE T1.LineStatus = 'o' GROUP BY COALESCE(T0.NumAtCard, T1.U_PurContNo), J1.itemcode ), -- 3. 计算在途总量 in_transit_total AS ( SELECT ItemCode AS 物料编码, SUM(未到货数量) AS 在途总量 FROM ( -- 报价单数量 SELECT n1.ItemCode, SUM(n1.Quantity) AS 未到货数量 FROM SINO_SAP.dbo.OPQT n left JOIN SINO_SAP.dbo.PQT1 n1 ON n.DocEntry = n1.DocEntry left JOIN SINO_SAP.dbo.OUDP p ON n1.U_Department = p.Name WHERE n.CANCELED = 'n' AND p.Remarks = '经销体系' AND n1.U_SaleContNo = '备货' AND COALESCE(n.NumAtCard, n1.U_PurContNo) IS NOT NULL AND n.DocDate >= '2024-01-01' GROUP BY n1.ItemCode UNION ALL -- 实际到货(负向) SELECT n1.ItemCode, -SUM(n1.Quantity) FROM SINO_SAP.dbo.OPDN n left JOIN SINO_SAP.dbo.PDN1 n1 ON n.DocEntry = n1.DocEntry left JOIN SINO_SAP.dbo.OUDP p ON n1.U_Department = p.Name WHERE n.CANCELED = 'n' AND p.Remarks = '经销体系' AND n1.U_SaleContNo = '备货' AND COALESCE(n.NumAtCard, n1.U_PurContNo) IS NOT NULL AND n.NumAtCard NOT LIKE '%CFBZ%' AND n.U_UpTransNo IS NULL GROUP BY n1.ItemCode UNION ALL -- 退货(负向) SELECT n1.ItemCode, SUM(n1.Quantity) FROM SINO_SAP.dbo.ORPD n left JOIN SINO_SAP.dbo.RPD1 n1 ON n.DocEntry = n1.DocEntry left JOIN SINO_SAP.dbo.OUDP p ON n1.U_Department = p.Name WHERE n.CANCELED = 'n' AND p.Remarks = '经销体系' AND n1.U_SaleContNo = '备货' AND COALESCE(n.NumAtCard, n1.U_PurContNo) IS NOT NULL AND n.NumAtCard NOT LIKE '%CFBZ%' AND n.U_UpTransNo IS NULL GROUP BY n1.ItemCode UNION ALL -- 贷项(负向) SELECT n1.ItemCode, SUM(n1.Quantity) FROM SINO_SAP.dbo.ORPC n left JOIN SINO_SAP.dbo.RPC1 n1 ON n.DocEntry = n1.DocEntry left JOIN SINO_SAP.dbo.OUDP p ON n1.U_Department = p.Name WHERE n.CANCELED = 'n' AND p.Remarks = '经销体系' AND n1.U_SaleContNo = '备货' AND COALESCE(n.NumAtCard, n1.U_PurContNo) IS NOT NULL AND n.NumAtCard NOT LIKE '%CFBZ%' AND n.U_UpTransNo IS NULL GROUP BY n1.ItemCode ) t GROUP BY ItemCode HAVING SUM(未到货数量) > 0 ), -- 4. 计算现缺数量 shortage AS ( SELECT o.采购合同号, o.物料编码, o.最早订单日期, o.订单数量, o.采购单据号, COALESCE(a.占货数量, 0) AS 占货数量, -- 用CASE替换GREATEST CASE WHEN o.订单数量 - COALESCE(a.占货数量, 0) > 0 THEN o.订单数量 - COALESCE(a.占货数量, 0) ELSE 0 END AS 现缺数量, COALESCE(a.占货数量, 0) * 1.0 / NULLIF(o.订单数量, 0) AS 占货率 FROM order_qty o LEFT JOIN alloc_qty a ON o.采购合同号 = a.采购合同号 AND o.物料编码 = a.物料编码 ), -- 5. 分配在途数量 alloc_in_transit AS ( SELECT s.*, COALESCE(it.在途总量, 0) AS 在途总量, -- 按时间顺序分配在途 (FIFO) CASE WHEN SUM(s.现缺数量) OVER ( PARTITION BY s.物料编码 ORDER BY s.最早订单日期, s.采购合同号 ) <= COALESCE(it.在途总量, 0) THEN s.现缺数量 WHEN COALESCE(it.在途总量, 0) > COALESCE(SUM(s.现缺数量) OVER ( PARTITION BY s.物料编码 ORDER BY s.最早订单日期, s.采购合同号 ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ), 0) THEN COALESCE(it.在途总量, 0) - COALESCE(SUM(s.现缺数量) OVER ( PARTITION BY s.物料编码 ORDER BY s.最早订单日期, s.采购合同号 ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ), 0) ELSE 0 END AS 分配的在途数量 FROM shortage s LEFT JOIN in_transit_total it ON s.物料编码 = it.物料编码 ) -- 最终结果 SELECT 采购合同号 AS 售订单合同号, 物料编码, 最早订单日期, CAST(订单数量 AS INT) AS 订单数量, 采购单据号 AS 售订单单据号, CAST(占货数量 AS INT) 占货数量, CAST(现缺数量 AS INT) 现缺数量, CAST(在途总量 AS INT) 总在途数量, CAST(分配的在途数量 AS INT) 分配的在途数量, CAST(CASE WHEN 现缺数量 - 分配的在途数量 > 0 THEN 现缺数量 - 分配的在途数量 ELSE 0 END AS INT) 需下单数量 FROM alloc_in_transit ORDER BY 物料编码, 最早订单日期, 采购合同号; – END 这个代码有较大的改动 改动的就是订单表 后续的分配逻辑不变 最终结果把订单的信息都带出来 订单的改动如下SELECT a.SupplCode or号, a.采购合同号 合同号, a.物料编码 物料编码, a.品牌 品牌, a.产品线 产品线, a.订单日期 过账日期, OSLP.slpname 销售员, r.U_Department 部门, r.U_company 公司, r.DocEntry 单据号, OCRD.CardName 客户名称, OCRD.U_province 客户省份, a.订单数量 订单数量 FROM ( SELECT n.SupplCode, MIN(COALESCE(n.NumAtCard, n1.U_PurContNo)) AS 采购合同号, n1.ItemCode AS 物料编码, MIN(n.DocDate) AS 订单日期, SUM(n1.Quantity) AS 订单数量, MIN(n1.U_Brand) 品牌, MIN(n1.U_prodline) 产品线 FROM SINO_SAP.dbo.ORDR n INNER JOIN SINO_SAP.dbo.RDR1 n1 ON n.DocEntry = n1.DocEntry INNER JOIN SINO_SAP.dbo.OUDP p ON n1.U_Department = p.Name AND p.Remarks = ‘经销体系’ WHERE n1.LineStatus = ‘o’ AND n.DocDate >= ‘2024-01-01’ AND COALESCE(n.NumAtCard, n1.U_PurContNo) IS NOT NULL AND n.cardcode NOT IN (‘C005596’,‘C003433’) AND NOT EXISTS ( SELECT 1 FROM [ZY_VIEW_XM] xm WHERE n.NumAtCard = xm.xm_yn OR n1.U_PurContNo = xm.xm_yn ) GROUP BY n.SupplCode, n1.ItemCode ) a LEFT JOIN SINO_SAP.dbo.ORDR r ON a.SupplCode = r.SupplCode LEFT JOIN OSLP ON r.slpcode = OSLP.SlpCode LEFT JOIN OCRD ON r.cardcode = OCRD.cardcode; 占货也有变化 SELECT T0.SupplCode, J1.itemcode AS 物料编码, SUM(J1.ALLocqty) AS 占货数量 FROM SINO_SAP.dbo.ORDR T0 left JOIN SINO_SAP.dbo.RDR1 T1 ON T0.DocEntry = T1.DocEntry left JOIN SINO_SAP.dbo.OITL J0 ON J0.DocEntry = T1.DocEntry AND J0.DocType = '17' AND J0.DocLine = T1.LineNum JOIN SINO_SAP.dbo.ITL1 J1 ON J1.LogEntry = J0.LogEntry WHERE T1.LineStatus = 'o' GROUP BY T0.SupplCode, J1.itemcode ,后续的计算分配逻辑不变,所有的内容都以or号为关键字段

WITH tmp17 as ( SELECT DISTINCT jk. * FROM m_number_of_information_category jk INNER JOIN m_pattern pt ON jk.pattern_type = pt.pattern_type AND jk.pattern_code = pt.pattern_code AND pt.version = $1 AND pt.original_store_code = $2 AND TO_DATE($3, 'YYYY-MM-DD') BETWEEN pt.apply_start_date AND pt.apply_end_date WHERE jk.version = $4 AND TO_DATE($5, 'YYYY-MM-DD') BETWEEN jk.apply_start_date AND jk.apply_end_date AND ( jk.license_code = '00' OR jk.license_code IN ( $6 , $7 , $8 , $9 , $10 , $11 , $12 , $13 , $14 , $15 , $16 , $17 , $18 , $19 , $20 , $21 , $22 , $23 , $24 , $25 ) ) AND jk.reading_item_quantity > 0 AND jk.specific_item_flag = '0' UNION SELECT DISTINCT jk. * FROM m_number_of_information_category jk INNER JOIN m_pattern pt ON jk.pattern_type = pt.pattern_type AND jk.pattern_code = pt.pattern_code AND pt.version = $26 AND pt.original_store_code = $27 AND TO_DATE($28, 'YYYY-MM-DD') BETWEEN pt.apply_start_date AND pt.apply_end_date INNER JOIN m_item sm ON jk.pattern_type = sm.pattern_type AND jk.pattern_code = sm.pattern_code AND jk.host_cycle_code = sm.host_cycle_code AND jk.store_cycle_code = sm.store_cycle_code AND jk.information_category_code = sm.information_category_code AND sm.version = $29 AND TO_DATE($30, 'YYYY-MM-DD') BETWEEN sm.apply_start_date AND sm.apply_end_date AND sm.specific_item_type IN ('1', 'G') INNER JOIN m_item_by_specific_store_recommendation sr ON pt.original_store_code = sr.original_store_code AND sm.item_code = sr.item_code AND sr.version = $31 AND TO_DATE($32, 'YYYY-MM-DD') BETWEEN sr.apply_start_date AND sr.apply_end_date WHERE jk.version = $33 AND TO_DATE($34, 'YYYY-MM-DD') BETWEEN jk.apply_start_date AND jk.apply_end_date AND ( jk.license_code = '00' OR jk.license_code IN ( $35 , $36 , $37 , $38 , $39 , $40 , $41 , $42 , $43 , $44 , $45 , $46 , $47 , $48 , $49 , $50 , $51 , $52 , $53 , $54 ) ) AND jk.reading_item_quantity > 0 AND jk.specific_item_flag = '1' ) , tmp3 as ( SELECT hg.host_cycle_code ,hg.store_cycle_code ,SUM(hg.reading_item_quantity) AS reading_item_quantity ,sc.store_cycle_name ,sc.cycle_display_order ,sc.order_method_type ,pt.original_store_code ,hg.pattern_type ,hg.pattern_code FROM m_number_of_order_group hg INNER JOIN m_pattern pt ON hg.pattern_type = pt.pattern_type AND hg.pattern_code = pt.pattern_code AND pt.version = $55 AND pt.original_store_code = $56 AND TO_DATE($57, 'YYYY-MM-DD') BETWEEN pt.apply_start_date AND pt.apply_end_date INNER JOIN m_cycle sc ON hg.host_cycle_code = sc.host_cycle_code AND hg.store_cycle_code = sc.store_cycle_code AND sc.version = $58 AND sc.order_method_type IN ('02','03','04','05','06','07','09','10') AND TO_DATE($59, 'YYYY-MM-DD') BETWEEN sc.apply_start_date AND sc.apply_end_date WHERE hg.version = $60 AND TO_DATE($61, 'YYYY-MM-DD') BETWEEN hg.apply_start_date AND hg.apply_end_date AND hg.specific_item_flag = '0' AND ( hg.license_code = '00' OR hg.license_code IN ( $62 , $63 , $64 , $65 , $66 , $67 , $68 , $69 , $70 , $71 , $72 , $73 , $74 , $75 , $76 , $77 , $78 , $79 , $80 , $81 ) ) GROUP BY hg.host_cycle_code , hg.store_cycle_code , sc.store_cycle_name , sc.cycle_display_order , sc.order_method_type , pt.original_store_code , hg.pattern_type , hg.pattern_code UNION SELECT hg.host_cycle_code, hg.store_cycle_code, SUM(hg.reading_item_quantity) AS reading_item_quantity, sc.store_cycle_name, sc.cycle_display_order, sc.order_method_type, pt.original_store_code, hg.pattern_type, hg.pattern_code FROM m_number_of_order_group hg INNER JOIN m_pattern pt ON hg.pattern_type = pt.pattern_type AND hg.pattern_code = pt.pattern_code AND pt.version = $82 AND pt.original_store_code = $83 AND TO_DATE($84, 'YYYY-MM-DD') BETWEEN pt.apply_start_date AND pt.apply_end_date INNER JOIN m_cycle sc ON hg.host_cycle_code = sc.host_cycle_code AND hg.store_cycle_code = sc.store_cycle_code AND sc.version = $85 AND sc.order_method_type IN ('02', '03', '04', '05', '06', '07', '09', '10') AND TO_DATE($86, 'YYYY-MM-DD') BETWEEN sc.apply_start_date AND sc.apply_end_date INNER JOIN m_item sm ON hg.pattern_type = sm.pattern_type AND hg.pattern_code = sm.pattern_code AND hg.host_cycle_code = sm.host_cycle_code AND hg.store_cycle_code = sm.store_cycle_code AND sm.version = $87 AND TO_DATE($88, 'YYYY-MM-DD') BETWEEN sm.apply_start_date AND sm.apply_end_date AND sm.specific_item_type IN ('1', 'G') INNER JOIN m_item_by_specific_store_recommendation sr ON pt.original_store_code = sr.original_store_code AND sm.item_code = sr.item_code AND sr.version = $89 AND TO_DATE($90, 'YYYY-MM-DD') BETWEEN sr.apply_start_date AND sr.apply_end_date WHERE hg.version = $91 AND TO_DATE($92, 'YYYY-MM-DD') BETWEEN hg.apply_start_date AND hg.apply_end_date AND hg.specific_item_flag = '1' AND ( hg.license_code = '00' OR hg.license_code IN ( $93 , $94 , $95 , $96 , $97 , $98 , $99 , $100 , $101 , $102 , $103 , $104 , $105 , $106 , $107 , $108 , $109 , $110 , $111 , $112 ) ) GROUP BY hg.host_cycle_code, hg.store_cycle_code, sc.store_cycle_name, sc.cycle_display_order, sc.order_method_type, pt.original_store_code, hg.pattern_type, hg.pattern_code ) , tmp4 as ( SELECT DISTINCT tmp3.host_cycle_code ,tmp3.store_cycle_code ,tmp3.store_cycle_name ,tmp3.cycle_display_order ,tmp3.order_method_type ,tmp3.original_store_code FROM tmp3 INNER JOIN m_order_schedule hs ON tmp3.pattern_type = hs.pattern_type AND tmp3.pattern_code = hs.pattern_code AND tmp3.host_cycle_code = hs.host_cycle_code AND tmp3.store_cycle_code = hs.store_cycle_code AND hs.version = $113 AND hs.order_date = TO_DATE($114, 'YYYY-MM-DD') LEFT OUTER JOIN m_delivery_type nk ON hs.host_cycle_code = nk.host_cycle_code AND hs.delivery_schedule_code = nk.delivery_schedule_code AND nk.version = $115 AND nk.original_store_code = $116 AND TO_DATE($117, 'YYYY-MM-DD') BETWEEN nk.apply_start_date AND nk.apply_end_date WHERE tmp3.reading_item_quantity > 0 AND (hs.delivery_schedule_code = '0' OR (hs.delivery_schedule_code != '0' AND nk.delivery_schedule_code IS NOT NULL)) ORDER BY tmp3.cycle_display_order ASC) , tmp5 as ( SELECT DISTINCT tmp4.original_store_code ,tmp4.host_cycle_code ,tmp4.store_cycle_code ,(CASE WHEN COALESCE(hk.setting_order_action_flag, '0') = '1' THEN '1' ELSE '0' END) AS setting_order_action_flag ,(CASE WHEN tmp4.order_method_type IN ('09','10') AND COALESCE(hk.daily_recommendation_quantity_of_stock_distribution_flag,'0') = '1' AND COALESCE(hk.daily_ai_order_action_flag,'0') = '1' THEN '1' WHEN tmp4.order_method_type IN ('04','05','06') AND COALESCE(hk.not_daily_ai_order_action_flag,'0') = '1' THEN '1' ELSE '0' END) AS ai_order_action_flag FROM tmp4 LEFT OUTER JOIN m_order_basic_setting hk ON tmp4.original_store_code = hk.original_store_code WHERE tmp4.order_method_type IN ('04','05','06','09','10')) , tmp6 as ( SELECT DISTINCT tmp4.host_cycle_code ,tmp4.store_cycle_code ,jt.information_category_code ,jm.information_category_name ,jt.staff_code ,MIN(COALESCE(ik.group_number, ip.group_number)) AS reading_number ,MIN(COALESCE(ik.setting_order, ip.setting_order)) AS setting_order FROM m_staff_by_information_order jt INNER JOIN tmp4 ON jt.original_store_code = tmp4.original_store_code AND jt.host_cycle_code = tmp4.host_cycle_code AND jt.store_cycle_code = tmp4.store_cycle_code LEFT OUTER JOIN m_information_category jm ON jm.version = $118 AND jt.information_category_code = jm.information_category_code AND TO_DATE($119, 'YYYY-MM-DD') BETWEEN jm.apply_start_date AND jm.apply_end_date INNER JOIN tmp17 jk ON tmp4.host_cycle_code = jk.host_cycle_code AND tmp4.store_cycle_code = jk.store_cycle_code AND jt.information_category_code = jk.information_category_code INNER JOIN m_reading_number_by_pattern ip ON ip.pattern_type = jk.pattern_type AND ip.pattern_code = jk.pattern_code AND jt.host_cycle_code = ip.host_cycle_code AND jt.store_cycle_code = ip.store_cycle_code AND jt.information_category_code = ip.information_category_code AND ip.version = $120 AND TO_DATE ($121, 'YYYY-MM-DD' ) BETWEEN ip.apply_start_date AND ip.apply_end_date LEFT OUTER JOIN m_reading_number_by_store ik ON jt.host_cycle_code = ik.host_cycle_code AND jt.store_cycle_code = ik.store_cycle_code AND jt.information_category_code = ik.information_category_code AND ik.original_store_code = $122 AND TO_DATE ($123, 'YYYY-MM-DD' ) BETWEEN ik.apply_start_date AND ik.apply_end_date GROUP BY tmp4.host_cycle_code, tmp4.store_cycle_code, jt.information_category_code, jm.information_category_name, jt.staff_code ) , tmp7 as ( SELECT tmp4.host_cycle_code ,tmp4.store_cycle_code ,st.information_category_code ,jm.information_category_name ,jm.information_category_display_order ,st.staff_code FROM m_staff_by_other_order st INNER JOIN tmp4 ON st.original_store_code = tmp4.original_store_code AND st.host_cycle_code = tmp4.host_cycle_code AND st.store_cycle_code = tmp4.store_cycle_code LEFT OUTER JOIN m_information_category jm ON jm.version = $124 AND st.information_category_code = jm.information_category_code AND TO_DATE($125, 'YYYY-MM-DD') BETWEEN jm.apply_start_date AND jm.apply_end_date INNER JOIN tmp17 jk ON tmp4.host_cycle_code = jk.host_cycle_code AND tmp4.store_cycle_code = jk.store_cycle_code AND st.information_category_code = jk.information_category_code ) , tmp8 as ( SELECT tmp4.host_cycle_code ,tmp4.store_cycle_code ,gt.gondola_number ,gm.information_category_code ,jm.information_category_name ,gt.staff_code FROM m_staff_by_gondola_order gt INNER JOIN m_number_of_gondola gm ON gt.original_store_code = gm.original_store_code AND gt.host_cycle_code = gm.host_cycle_code AND gt.store_cycle_code = gm.store_cycle_code AND gt.gondola_number = gm.gondola_number AND gm.version = $126 AND TO_DATE($127, 'YYYY-MM-DD') BETWEEN gm.apply_start_date AND gm.apply_end_date AND gm.reading_item_quantity > 0 INNER JOIN tmp4 ON gt.original_store_code = tmp4.original_store_code AND gt.host_cycle_code = tmp4.host_cycle_code AND gt.store_cycle_code = tmp4.store_cycle_code LEFT OUTER JOIN m_information_category jm ON jm.version = $128 AND gm.information_category_code = jm.information_category_code AND TO_DATE($129, 'YYYY-MM-DD') BETWEEN jm.apply_start_date AND jm.apply_end_date WHERE ( gm.license_code = '00' OR gm.license_code IN ( $130 , $131 , $132 , $133 , $134 , $135 , $136 , $137 , $138 , $139 , $140 , $141 , $142 , $143 , $144 , $145 , $146 , $147 , $148 , $149 ) ) ) , tmp9 as ( SELECT tmp8.host_cycle_code ,tmp8.store_cycle_code ,tmp8.gondola_number ,tmp8.information_category_code ,tmp8.staff_code ,tt.information_category_code AS display_input_information_category_code FROM tmp8 LEFT OUTER JOIN tmp6 ON tmp8.host_cycle_code = tmp6.host_cycle_code AND tmp8.store_cycle_code = tmp6.store_cycle_code AND tmp8.information_category_code = tmp6.information_category_code AND tmp8.staff_code = tmp6.staff_code LEFT OUTER JOIN m_specific_display_order tt ON tmp8.gondola_number = tt.display_gondola AND tmp8.information_category_code = tt.information_category_code AND tt.original_store_code = $150 WHERE tmp6.information_category_code IS NULL) , tmp10 as ( SELECT tmp6.host_cycle_code ,tmp6.store_cycle_code ,tmp6.information_category_code ,tmp6.staff_code FROM tmp6 UNION SELECT tmp7.host_cycle_code ,tmp7.store_cycle_code ,tmp7.information_category_code ,tmp7.staff_code FROM tmp7 UNION SELECT DISTINCT tmp9.host_cycle_code ,tmp9.store_cycle_code ,tmp9.information_category_code ,tmp9.staff_code FROM tmp9 WHERE tmp9.display_input_information_category_code IS NOT NULL) , tmp11 as( SELECT DISTINCT tmp9.host_cycle_code ,tmp9.store_cycle_code ,tmp9.staff_code ,tj.item_code FROM tmp9 INNER JOIN m_item sm ON tmp9.host_cycle_code = sm.host_cycle_code AND tmp9.store_cycle_code = sm.store_cycle_code AND tmp9.information_category_code = sm.information_category_code AND sm.version = $151 AND TO_DATE($152, 'YYYY-MM-DD') BETWEEN sm.apply_start_date AND sm.apply_end_date INNER JOIN m_pattern pt ON sm.pattern_type = pt.pattern_type AND sm.pattern_code = pt.pattern_code AND pt.version = $153 AND pt.original_store_code = $154 AND TO_DATE($155, 'YYYY-MM-DD') BETWEEN pt.apply_start_date AND pt.apply_end_date INNER JOIN m_display tj ON sm.item_code = tj.item_code AND tj.original_store_code = $156 AND tmp9.gondola_number = tj.display_gondola WHERE tmp9.display_input_information_category_code IS NULL) , tmp12 as( SELECT tmp10.host_cycle_code ,tmp10.store_cycle_code ,tmp10.staff_code ,SUM(hz1.input_packed_quantity) AS order_total_quantity ,SUM(hz1.input_amount) AS order_total_amount ,MAX(hz1.order_done_display_flag) AS order_situation FROM s_order hz1 INNER JOIN tmp10 ON hz1.host_cycle_code = tmp10.host_cycle_code AND hz1.store_cycle_code = tmp10.store_cycle_code AND hz1.information_category_code = tmp10.information_category_code AND hz1.original_store_code = '181013' AND hz1.order_date = TO_DATE($157, 'YYYY-MM-DD') GROUP BY tmp10.host_cycle_code, tmp10.store_cycle_code, tmp10.staff_code) , tmp13 as ( SELECT tmp11.host_cycle_code ,tmp11.store_cycle_code ,tmp11.staff_code ,SUM(hz2.input_packed_quantity) AS order_total_quantity ,SUM(hz2.input_amount) AS order_total_amount ,MAX(hz2.order_done_display_flag) AS order_situation FROM s_order hz2 INNER JOIN tmp11 ON hz2.item_code = tmp11.item_code AND hz2.original_store_code = '181013' AND hz2.order_date = TO_DATE($158, 'YYYY-MM-DD') GROUP BY tmp11.host_cycle_code, tmp11.store_cycle_code, tmp11.staff_code) , tmp18 as ( SELECT tmp6.host_cycle_code ,tmp6.store_cycle_code ,tmp6.information_category_code ,tmp6.staff_code FROM tmp6 UNION SELECT tmp7.host_cycle_code ,tmp7.store_cycle_code ,tmp7.information_category_code ,tmp7.staff_code FROM tmp7 UNION SELECT DISTINCT tmp9.host_cycle_code ,tmp9.store_cycle_code ,tmp9.information_category_code ,tmp9.staff_code FROM tmp9) , tmp14 as ( SELECT DISTINCT tmp4.host_cycle_code ,tmp4.store_cycle_code FROM tmp4 WHERE NOT EXISTS (SELECT 1 FROM tmp18 WHERE tmp18.host_cycle_code = tmp4.host_cycle_code AND tmp18.store_cycle_code = tmp4.store_cycle_code)) , tmp15 as ( SELECT tmp14.host_cycle_code ,tmp14.store_cycle_code ,NULL AS staff_code ,SUM(hz3.input_packed_quantity) AS order_total_quantity ,SUM(hz3.input_amount) AS order_total_amount ,MAX(hz3.order_done_display_flag) AS order_situation FROM s_order hz3 INNER JOIN tmp14 ON hz3.host_cycle_code = tmp14.host_cycle_code AND hz3.store_cycle_code = tmp14.store_cycle_code AND hz3.original_store_code = '181013' AND hz3.order_date = TO_DATE($159, 'YYYY-MM-DD') GROUP BY tmp14.host_cycle_code,tmp14.store_cycle_code) , tmp16 as ( SELECT tmp16_tmp.host_cycle_code , tmp16_tmp.store_cycle_code , tmp16_tmp.staff_code , SUM(tmp16_tmp.order_total_quantity) as order_total_quantity , SUM(tmp16_tmp.order_total_amount) as order_total_amount , MAX(tmp16_tmp.order_situation) as order_situation FROM ( SELECT tmp12.host_cycle_code , tmp12.store_cycle_code , tmp12.staff_code , tmp12.order_total_quantity , tmp12.order_total_amount , tmp12.order_situation FROM tmp12 UNION SELECT tmp13.host_cycle_code , tmp13.store_cycle_code , tmp13.staff_code , tmp13.order_total_quantity , tmp13.order_total_amount , tmp13.order_situation FROM tmp13 UNION SELECT tmp15.host_cycle_code , tmp15.store_cycle_code , tmp15.staff_code , tmp15.order_total_quantity , tmp15.order_total_amount , tmp15.order_situation FROM tmp15 ) AS tmp16_tmp GROUP BY tmp16_tmp.host_cycle_code , tmp16_tmp.store_cycle_code , tmp16_tmp.staff_code ) SELECT DISTINCT tmp18.staff_code AS order_staff_code ,tmp4.store_cycle_name AS order_group_name ,tmp4.host_cycle_code ,tmp4.store_cycle_code ,COALESCE(tmp16.order_situation, '0') AS order_situation ,COALESCE(tmp16.order_total_quantity, 0) AS order_total_quantity ,COALESCE(tmp16.order_total_amount, 0) AS order_total_amount ,(CASE WHEN rz.order_way = '1' AND tmp5.setting_order_action_flag = '1' THEN '1' WHEN rz.order_way = '2' AND tmp5.ai_order_action_flag = '1' THEN '2' ELSE '0' END ) AS order_way_first ,('1' || COALESCE(tmp5.setting_order_action_flag, '0') || COALESCE(tmp5.ai_order_action_flag, '0')) AS selection_permission_order_way ,CASE WHEN tmp4.order_method_type = '07' THEN '0' ELSE COALESCE(rz.displayorder, '0') END AS sort_method_first ,('1' || '1' || '1') AS selection_permission_sort_method ,(CASE WHEN rz.reading_method IS NOT NULL AND rz.reading_method = '0' AND ici_tmp.information_category_code_information_list IS NOT NULL THEN '0' WHEN rz.reading_method IS NOT NULL AND rz.reading_method = '1' AND icg_tmp.gondola_number_list IS NOT NULL THEN '1' WHEN yt.reading_method IS NOT NULL THEN (CASE WHEN yt.reading_method = '1' THEN '0' WHEN yt.reading_method = '2' THEN '1' END) WHEN ici_tmp.information_category_code_information_list IS NOT NULL AND icg_tmp.gondola_number_list IS NULL THEN '0' WHEN ici_tmp.information_category_code_information_list IS NULL AND icg_tmp.gondola_number_list IS NOT NULL THEN '1' WHEN tmp4.order_method_type IN ('04', '05', '06', '09') THEN '1' ELSE '0' END) AS reading_method_first ,('1') || (CASE WHEN tmp4.order_method_type IN ('04', '05', '06', '09') THEN '1' ELSE '0' END) AS selection_permission_reading_method ,ici_tmp.information_category_code_information_list ,ici_tmp.information_category_name_information_list ,ici_tmp.reading_number_information_list ,ici_tmp.setting_order_information_list ,icg_tmp.information_category_code_gondola_list ,icg_tmp.information_category_name_gondola_list ,icg_tmp.information_category_display_order_list ,icg_tmp.gondola_number_list ,icg_tmp.other_information_category_flag_list ,icg_tmp.unregistered_information_category_flag_list ,tmp4.order_method_type ,tmp4.cycle_display_order FROM tmp4 LEFT OUTER JOIN tmp18 ON tmp18.host_cycle_code = tmp4.host_cycle_code AND tmp18.store_cycle_code = tmp4.store_cycle_code LEFT OUTER JOIN m_staff_by_reading_method yt ON tmp4.host_cycle_code = yt.host_cycle_code AND tmp4.store_cycle_code = yt.store_cycle_code AND yt.original_store_code = $160 AND yt.staff_code = tmp18.staff_code LEFT OUTER JOIN tmp16 ON tmp4.host_cycle_code = tmp16.host_cycle_code AND tmp4.store_cycle_code = tmp16.store_cycle_code AND (CASE WHEN tmp16.staff_code IS NULL THEN tmp18.staff_code IS NULL ELSE tmp18.staff_code = tmp16.staff_code END ) LEFT OUTER JOIN s_cycle_select_history rz ON tmp4.host_cycle_code = rz.host_cycle_code AND tmp4.store_cycle_code = rz.store_cycle_code AND ( CASE WHEN rz.staff_code = '0000000000000' THEN tmp18.staff_code IS NULL ELSE tmp18.staff_code = rz.staff_code END ) AND rz.original_store_code = $161 AND rz.login_employee_code = $162 LEFT OUTER JOIN tmp5 ON tmp4.host_cycle_code = tmp5.host_cycle_code AND tmp4.store_cycle_code = tmp5.store_cycle_code AND tmp5.original_store_code = $163 LEFT OUTER JOIN ( SELECT ici.host_cycle_code , ici.store_cycle_code , array_agg(ici.information_category_code) AS information_category_code_information_list , array_agg(ici.information_category_name) AS information_category_name_information_list , array_agg(ici.reading_number) AS reading_number_information_list , array_agg(ici.setting_order) AS setting_order_information_list , ici.staff_code FROM ( SELECT tmp6.host_cycle_code , tmp6.store_cycle_code , tmp6.information_category_code , tmp6.information_category_name , tmp6.staff_code , tmp6.reading_number AS reading_number , tmp6.setting_order AS setting_order FROM tmp6 UNION SELECT DISTINCT jk.host_cycle_code , jk.store_cycle_code , jk.information_category_code , jm.information_category_name , t6_tmp.staff_code , MIN(COALESCE(ik.group_number, ip.group_number)) AS reading_number , MIN(COALESCE(ik.setting_order, ip.setting_order)) AS setting_order FROM tmp17 jk LEFT OUTER JOIN m_staff_by_information_order jt ON jk.host_cycle_code = jt.host_cycle_code AND jk.store_cycle_code = jt.store_cycle_code AND jk.information_category_code = jt.information_category_code AND jt.original_store_code = $164 LEFT OUTER JOIN m_information_category jm ON jm.version = $165 AND jk.information_category_code = jm.information_category_code AND TO_DATE($166, 'YYYY-MM-DD') BETWEEN jm.apply_start_date AND jm.apply_end_date LEFT OUTER JOIN ( SELECT DISTINCT tmp6.host_cycle_code , tmp6.store_cycle_code , tmp6.staff_code FROM tmp6) t6_tmp ON jk.host_cycle_code = t6_tmp.host_cycle_code AND jk.store_cycle_code = t6_tmp.store_cycle_code INNER JOIN m_reading_number_by_pattern ip ON ip.pattern_type = jk.pattern_type AND ip.pattern_code = jk.pattern_code AND jk.host_cycle_code = ip.host_cycle_code AND jk.store_cycle_code = ip.store_cycle_code AND jk.information_category_code = ip.information_category_code AND ip.version = $167 AND TO_DATE($168, 'YYYY-MM-DD') BETWEEN ip.apply_start_date AND ip.apply_end_date LEFT OUTER JOIN m_reading_number_by_store ik ON jk.host_cycle_code = ik.host_cycle_code AND jk.store_cycle_code = ik.store_cycle_code AND jk.information_category_code = ik.information_category_code AND ik.original_store_code = $169 AND TO_DATE($170, 'YYYY-MM-DD') BETWEEN ik.apply_start_date AND ik.apply_end_date WHERE jt.information_category_code IS NULL GROUP BY jk.host_cycle_code, jk.store_cycle_code, jk.information_category_code, jm.information_category_name, t6_tmp.staff_code UNION SELECT DISTINCT jk.host_cycle_code, jk.store_cycle_code, jk.information_category_code, jm.information_category_name, tmp18.staff_code, MIN(COALESCE(ik.group_number, ip.group_number)) AS reading_number, MIN(COALESCE(ik.setting_order, ip.setting_order)) AS setting_order FROM tmp17 jk INNER JOIN tmp18 ON tmp18.host_cycle_code = jk.host_cycle_code AND tmp18.store_cycle_code = jk.store_cycle_code LEFT OUTER JOIN m_information_category jm ON jm.version = $171 AND jk.information_category_code = jm.information_category_code AND TO_DATE($172, 'YYYY-MM-DD') BETWEEN jm.apply_start_date AND jm.apply_end_date INNER JOIN m_reading_number_by_pattern ip ON ip.pattern_type = jk.pattern_type AND ip.pattern_code = jk.pattern_code AND jk.host_cycle_code = ip.host_cycle_code AND jk.store_cycle_code = ip.store_cycle_code AND jk.information_category_code = ip.information_category_code AND ip.version = $173 AND TO_DATE($174, 'YYYY-MM-DD') BETWEEN ip.apply_start_date AND ip.apply_end_date LEFT OUTER JOIN m_reading_number_by_store ik ON jk.host_cycle_code = ik.host_cycle_code AND jk.store_cycle_code = ik.store_cycle_code AND jk.information_category_code = ik.information_category_code AND ik.original_store_code = $175 AND TO_DATE($176, 'YYYY-MM-DD') BETWEEN ik.apply_start_date AND ik.apply_end_date LEFT OUTER JOIN m_staff_by_information_order jt ON jt.original_store_code = $177 AND jt.host_cycle_code = jk.host_cycle_code AND jt.store_cycle_code = jk.store_cycle_code AND jt.information_category_code = jk.information_category_code WHERE jt.staff_code IS NULL GROUP BY jk.host_cycle_code, jk.store_cycle_code, jk.information_category_code, jm.information_category_name, tmp18.staff_code ) ici GROUP BY ici.host_cycle_code , ici.store_cycle_code , ici.staff_code ) ici_tmp ON tmp4.host_cycle_code = ici_tmp.host_cycle_code AND tmp4.store_cycle_code = ici_tmp.store_cycle_code AND ( tmp18.staff_code = ici_tmp.staff_code OR (tmp18.staff_code IS NULL AND ici_tmp.staff_code IS NULL) ) LEFT OUTER JOIN ( SELECT icg.host_cycle_code , icg.store_cycle_code , array_agg(icg.information_category_code) AS information_category_code_gondola_list , array_agg(icg.information_category_name) AS information_category_name_gondola_list , icg.staff_code , array_agg(icg.information_category_display_order) AS information_category_display_order_list , array_agg(icg.gondola_number) AS gondola_number_list , array_agg(icg.other_information_category_flag) AS other_information_category_flag_list , array_agg(icg.unregistered_information_category_flag) AS unregistered_information_category_flag_list FROM ( SELECT tmp8.host_cycle_code , tmp8.store_cycle_code , tmp8.information_category_code , tmp8.information_category_name , 0 AS information_category_display_order , tmp8.staff_code , tmp8.gondola_number AS gondola_number , '0' AS other_information_category_flag , '0' AS unregistered_information_category_flag FROM tmp8 UNION SELECT tmp7.host_cycle_code , tmp7.store_cycle_code , tmp7.information_category_code , tmp7.information_category_name , tmp7.information_category_display_order AS information_category_display_order , tmp7.staff_code , 0 AS gondola_number , '1' AS other_information_category_flag , CASE WHEN gk.information_category_code IS NULL THEN '1' ELSE '0' END AS unregistered_information_category_flag FROM tmp7 LEFT OUTER JOIN m_number_of_gondola gk ON gk.version = $178 AND gk.original_store_code = $179 AND TO_DATE($180, 'YYYY-MM-DD') BETWEEN gk.apply_start_date AND gk.apply_end_date AND tmp7.host_cycle_code = gk.host_cycle_code AND tmp7.store_cycle_code = gk.store_cycle_code AND tmp7.information_category_code = gk.information_category_code AND ( gk.license_code = '00' OR gk.license_code IN ( $181 , $182 , $183 , $184 , $185 , $186 , $187 , $188 , $189 , $190 , $191 , $192 , $193 , $194 , $195 , $196 , $197 , $198 , $199 , $200 ) ) AND gk.reading_item_quantity > 0 UNION SELECT DISTINCT gk.host_cycle_code , gk.store_cycle_code , gk.information_category_code , jm.information_category_name , jm.information_category_display_order AS information_category_display_order , t7_t8_tmp1.staff_code , gk.gondola_number AS gondola_number , '0' AS other_information_category_flag , '0' AS unregistered_information_category_flag FROM m_number_of_gondola gk LEFT OUTER JOIN m_staff_by_gondola_order gt ON gk.host_cycle_code = gt.host_cycle_code AND gk.store_cycle_code = gt.store_cycle_code AND gk.gondola_number = gt.gondola_number AND gt.original_store_code = $201 LEFT OUTER JOIN m_staff_by_other_order st ON gk.host_cycle_code = st.host_cycle_code AND gk.store_cycle_code = st.store_cycle_code AND gk.information_category_code = st.information_category_code AND st.original_store_code = $202 LEFT OUTER JOIN m_information_category jm ON jm.version = $203 AND gk.information_category_code = jm.information_category_code AND TO_DATE($204, 'YYYY-MM-DD') BETWEEN jm.apply_start_date AND jm.apply_end_date LEFT OUTER JOIN ( SELECT DISTINCT tmp7.host_cycle_code , tmp7.store_cycle_code , tmp7.staff_code FROM tmp7 UNION SELECT DISTINCT tmp8.host_cycle_code , tmp8.store_cycle_code , tmp8.staff_code FROM tmp8 ) t7_t8_tmp1 ON gk.host_cycle_code = t7_t8_tmp1.host_cycle_code AND gk.store_cycle_code = t7_t8_tmp1.store_cycle_code WHERE gk.version = $205 AND gk.original_store_code = $206 AND TO_DATE($207, 'YYYY-MM-DD') BETWEEN gk.apply_start_date AND gk.apply_end_date AND ( gk.license_code = '00' OR gk.license_code IN ( $208 , $209 , $210 , $211 , $212 , $213 , $214 , $215 , $216 , $217 , $218 , $219 , $220 , $221 , $222 , $223 , $224 , $225 , $226 , $227 ) ) AND gk.reading_item_quantity > 0 AND gt.gondola_number IS NULL AND st.information_category_code IS NULL UNION SELECT DISTINCT jk.host_cycle_code , jk.store_cycle_code , jk.information_category_code , jm.information_category_name , jm.information_category_display_order AS information_category_display_order , t7_t8_tmp2.staff_code , 0 AS gondola_number , '0' AS other_information_category_flag , '1' AS unregistered_information_category_flag FROM tmp17 jk LEFT OUTER JOIN m_number_of_gondola gk ON jk.host_cycle_code = gk.host_cycle_code AND jk.store_cycle_code = gk.store_cycle_code AND jk.information_category_code = gk.information_category_code AND gk.version = $228 AND gk.original_store_code = $229 AND TO_DATE($230, 'YYYY-MM-DD') BETWEEN gk.apply_start_date AND gk.apply_end_date AND ( gk.license_code = '00' OR gk.license_code IN ( $231 , $232 , $233 , $234 , $235 , $236 , $237 , $238 , $239 , $240 , $241 , $242 , $243 , $244 , $245 , $246 , $247 , $248 , $249 , $250 ) ) AND gk.reading_item_quantity > 0 LEFT OUTER JOIN m_staff_by_other_order st ON jk.host_cycle_code = st.host_cycle_code AND jk.store_cycle_code = st.store_cycle_code AND jk.information_category_code = st.information_category_code AND st.original_store_code = $251 LEFT OUTER JOIN m_information_category jm ON jm.version = $252 AND jk.information_category_code = jm.information_category_code AND TO_DATE($253, 'YYYY-MM-DD') BETWEEN jm.apply_start_date AND jm.apply_end_date LEFT OUTER JOIN ( SELECT DISTINCT tmp7.host_cycle_code , tmp7.store_cycle_code , tmp7.staff_code FROM tmp7 UNION SELECT DISTINCT tmp8.host_cycle_code , tmp8.store_cycle_code , tmp8.staff_code FROM tmp8) t7_t8_tmp2 ON jk.host_cycle_code = t7_t8_tmp2.host_cycle_code AND jk.store_cycle_code = t7_t8_tmp2.store_cycle_code WHERE gk.information_category_code IS NULL AND st.information_category_code IS NULL UNION SELECT DISTINCT gk.host_cycle_code, gk.store_cycle_code, gk.information_category_code, jm.information_category_name, jm.information_category_display_order, tmp18.staff_code, gk.gondola_number, '0' AS other_information_category_flag, '0' AS unregistered_information_category_flag FROM m_number_of_gondola gk INNER JOIN tmp18 ON tmp18.host_cycle_code = gk.host_cycle_code AND tmp18.store_cycle_code = gk.store_cycle_code LEFT OUTER JOIN m_staff_by_gondola_order gt ON gk.host_cycle_code = gt.host_cycle_code AND gk.store_cycle_code = gt.store_cycle_code AND gk.gondola_number = gt.gondola_number AND gt.original_store_code = $254 LEFT OUTER JOIN m_staff_by_other_order st ON gk.host_cycle_code = st.host_cycle_code AND gk.store_cycle_code = st.store_cycle_code AND gk.information_category_code = st.information_category_code AND st.original_store_code = $255 LEFT OUTER JOIN m_information_category jm ON jm.version = $256 AND gk.information_category_code = jm.information_category_code AND TO_DATE($257, 'YYYY-MM-DD') BETWEEN jm.apply_start_date AND jm.apply_end_date WHERE gk.version = $258 AND gk.original_store_code = $259 AND TO_DATE($260, 'YYYY-MM-DD') BETWEEN gk.apply_start_date AND gk.apply_end_date AND ( gk.license_code = '00' OR gk.license_code IN ( $261 , $262 , $263 , $264 , $265 , $266 , $267 , $268 , $269 , $270 , $271 , $272 , $273 , $274 , $275 , $276 , $277 , $278 , $279 , $280 ) ) AND gk.reading_item_quantity > 0 AND gt.gondola_number IS NULL AND st.information_category_code IS NULL UNION SELECT DISTINCT jk.host_cycle_code, jk.store_cycle_code, jk.information_category_code, jm.information_category_name, jm.information_category_display_order, tmp18.staff_code, 0 AS gondola_number, '0' AS other_information_category_flag, '1' AS unregistered_information_category_flag FROM tmp17 jk INNER JOIN tmp18 ON tmp18.host_cycle_code = jk.host_cycle_code AND tmp18.store_cycle_code = jk.store_cycle_code LEFT OUTER JOIN m_number_of_gondola gk ON jk.host_cycle_code = gk.host_cycle_code AND jk.store_cycle_code = gk.store_cycle_code AND jk.information_category_code = gk.information_category_code AND gk.version = $281 AND gk.original_store_code = $282 AND TO_DATE($283, 'YYYY-MM-DD') BETWEEN gk.apply_start_date AND gk.apply_end_date AND ( gk.license_code = '00' OR gk.license_code IN ( $284 , $285 , $286 , $287 , $288 , $289 , $290 , $291 , $292 , $293 , $294 , $295 , $296 , $297 , $298 , $299 , $300 , $301 , $302 , $303 ) ) AND gk.reading_item_quantity > 0 LEFT OUTER JOIN m_staff_by_other_order st ON jk.host_cycle_code = st.host_cycle_code AND jk.store_cycle_code = st.store_cycle_code AND jk.information_category_code = st.information_category_code AND st.original_store_code = $304 LEFT OUTER JOIN m_information_category jm ON jm.version = $305 AND jk.information_category_code = jm.information_category_code AND TO_DATE($306, 'YYYY-MM-DD') BETWEEN jm.apply_start_date AND jm.apply_end_date WHERE gk.information_category_code IS NULL AND st.information_category_code IS NULL ) icg GROUP BY icg.host_cycle_code , icg.store_cycle_code , icg.staff_code ) icg_tmp ON tmp4.host_cycle_code = icg_tmp.host_cycle_code AND tmp4.store_cycle_code = icg_tmp.store_cycle_code AND tmp4.order_method_type IN ('04', '05', '06', '09') AND ( tmp18.staff_code = icg_tmp.staff_code OR (tmp18.staff_code IS NULL AND icg_tmp.staff_code IS NULL) ) ORDER BY tmp18.staff_code ASC, tmp4.cycle_display_order ASC SQL优化

class CommpoentsReader(BaseMainReader): """GitLab Merge Requests 读取器""" def __init__(self, db_utils: DBUtils): super().__init__(db_utils) @property def get_query(self) -> Callable: return self._get_raw_sql def _get_select_query(self) -> Select: metadata = MetaData() table = Table( "issues", metadata, schema="ods_sq", autoload_with=self.db_utils.get_engine_bind(), ) # 创建关联表的Table对象 metrics_table = Table( "components", metadata, schema="ods_sq", autoload_with=self.db_utils.get_engine_bind(), ) def _get_raw_sql(self) -> str: # 构建多表关联查询,使用DISTINCT避免重复记录 sql = f""" SELECT DISTINCT ON (com.uuid) com.uuid AS id, com.kee com.name AS file_name, com.path AS file_path, com.language AS file_language, com._task_id, com._tool_id FROM ods_sq.components com WHERE com.qualifiers = 'FIL' ORDER BY com.uuid DESC """ return text(sql) class LiveMeasuresReader(BaseRelationReader): """GitLab Labels 关联读取器""" def __init__(self, db_utils: DBUtils): super().__init__(db_utils) @property def relations(self) -> Dict[str, str]: return {"uuid": "component_uuid", "_tool_id": "_tool_id"} @property def get_query(self) -> Callable: return self._get_raw_sql def _get_select_query(self) -> Select: # live_measures表 table = self.db_utils.create_table_obj( table_name="live_measures", schema_name="ods_sq", ) # metrics表 metrics_table = self.db_utils.create_table_obj( table_name="metrics", schema_name="ods_sq", ) # 构建多个WHERE条件 where_conditions = [] for column, is_set in self.relation_ids.items(): if is_set and hasattr(table.c, column): if len(is_set) == 1: where_conditions.append( getattr(table.c, column) == next(iter(is_set)) ) elif len(is_set) > 1: where_conditions.append(getattr(table.c, column).in_(is_set)) query = ( select( table.c.metric_uuid, metrics_table.c.name.label("label_title"), table.c._task_id, table.c._tool_id, ) .select_from( table.outerjoin( metrics_table, (table.c.metric_uuid == metrics_table.c.uuid) & (table.c._tool_id == metrics_table.c._tool_id), ) ) .order_by(table.c.id, table.c._tool_id) ) if where_conditions: query = query.where(and_(*where_conditions)) return query def _get_raw_sql(self) -> str: # 构建多个WHERE条件 where_conditions = [] params = {} param_counter = 0 for column, id_set in self.relation_ids.items(): if id_set: if len(id_set) == 1: # 单个值使用参数绑定 param_name = f"comp_{param_counter}" where_conditions.append(f"rle.{column} = :{param_name}") params[param_name] = next(iter(id_set)) param_counter += 1 elif len(id_set) > 1: # 多个值使用IN子句和参数绑定 param_names = [] for value in id_set: param_name = f"param_{param_counter}" param_names.append(f":{param_name}") params[param_name] = value param_counter += 1 where_conditions.append( f"rle.{column} IN ({','.join(param_names)})" ) where_clause = ( f"WHERE {' AND '.join(where_conditions)}" if where_conditions else "" ) # 构建labels关联查询 sql = f""" SELECT com.uuid, com.name, l.value, m.name, l._task_id, l._tool_id FROM ods_sq.live_measures l LEFT JOIN ods_sq.metrics m ON l.metric_uuid = m.uuid AND l._tool_id = m._tool_id LEFT JOIN ods_sq.components com ON lm.component_uuid=com.components.uuid AND lm._tool_id=com.m._tool_id {where_clause} ORDER BY l.id, l._tool_id """ return text(sql).bindparams(**params)

/*WITH recursive_hierarchy AS ( SELECT l5.id, l5.name, l5.level, l5.parent_id as parent_l5, l4.id as id_l4, l4.parent_id as parent_l4, l3.id as id_l3, l3.parent_id as parent_l3, l2.id as id_l2, l2.parent_id as parent_l2, l1.id as id_l1 FROM view.ic_dev15oc_mart.view_dwd_plm_imp_plm_pm_prj_t_17010_hw_dev15oc_1054010535 l5 LEFT JOIN view.ic_dev15oc_mart.view_dwd_plm_imp_plm_pm_prj_t_17010_hw_dev15oc_1054010535 l4 ON l5.parent_id = l4.id AND l5.level = 5 LEFT JOIN view.ic_dev15oc_mart.view_dwd_plm_imp_plm_pm_prj_t_17010_hw_dev15oc_1054010535 l3 ON COALESCE(l4.parent_id, l5.parent_id) = l3.id AND l5.level >= 4 LEFT JOIN view.ic_dev15oc_mart.view_dwd_plm_imp_plm_pm_prj_t_17010_hw_dev15oc_1054010535 l2 ON COALESCE(l3.parent_id, l4.parent_id, l5.parent_id) = l2.id AND l5.level >= 3 LEFT JOIN view.ic_dev15oc_mart.view_dwd_plm_imp_plm_pm_prj_t_17010_hw_dev15oc_1054010535 l1 ON COALESCE(l2.parent_id, l3.parent_id, l4.parent_id, l5.parent_id) = l1.id AND l5.level >= 2 )*/ SELECT id, name, level, -- parent_id, CASE WHEN level = 1 THEN CAST(id AS STRING) WHEN level = 2 THEN CONCAT_WS('.',CAST(id_l1 AS STRING) , CAST(id AS STRING)) WHEN level = 3 THEN CONCAT_WS('.',CAST(id_l1 AS STRING),CAST(id_l2 AS STRING), CAST(id AS STRING)) WHEN level = 4 THEN CONCAT_WS('.', CAST(id_l1 AS STRING), CAST(id_l2 AS STRING), CAST(id_l3 AS STRING), CAST(id AS STRING)) WHEN level = 5 THEN CONCAT_WS('.', CAST(id_l1 AS STRING), CAST(id_l2 AS STRING), CAST(id_l3 AS STRING), CAST(id_l4 AS STRING), CAST(id AS STRING)) END AS sort FROM ( SElECT l5.id, l5.name, l5.level, l5.parent_id as parent_l5, l4.id as id_l4, l4.parent_id as parent_l4, l3.id as id_l3, l3.parent_id as parent_l3, l2.id as id_l2, l2.parent_id as parent_l2, l1.id as id_l1 FROM view.ic_dev15oc_mart.view_dwd_plm_imp_plm_pm_prj_t_17010_hw_dev15oc_1054010535 l5 LEFT JOIN view.ic_dev15oc_mart.view_dwd_plm_imp_plm_pm_prj_t_17010_hw_dev15oc_1054010535 l4 ON l5.parent_id = l4.id AND l5.level = 5 LEFT JOIN view.ic_dev15oc_mart.view_dwd_plm_imp_plm_pm_prj_t_17010_hw_dev15oc_1054010535 l3 ON COALESCE(l4.parent_id, l5.parent_id) = l3.id AND l5.level >= 4 LEFT JOIN view.ic_dev15oc_mart.view_dwd_plm_imp_plm_pm_prj_t_17010_hw_dev15oc_1054010535 l2 ON COALESCE(l3.parent_id, l4.parent_id, l5.parent_id) = l2.id AND l5.level >= 3 LEFT JOIN view.ic_dev15oc_mart.view_dwd_plm_imp_plm_pm_prj_t_17010_hw_dev15oc_1054010535 l1 ON COALESCE(l2.parent_id, l3.parent_id, l4.parent_id, l5.parent_id) = l1.id AND l5.level >= 2 )tt;详细解读一下left join 部分,我不是很理解,越详细越好

最新推荐

recommend-type

开发界面语义化:声控 + 画图协同生成代码.doc

开发界面语义化:声控 + 画图协同生成代码.doc
recommend-type

LABVIEW与三菱PLC通信:实现数据批量读写的高效库解决方案

如何通过LabVIEW与三菱PLC建立高效的通信桥梁,实现数据批量读写。首先概述了LabVIEW和三菱PLC的基本概念及其在工业自动化中的重要性。接着重点讲解了利用Modbus RTU协议构建通信连接的具体步骤和技术细节,包括初始化通信、发送读写请求、处理响应数据和关闭连接等功能。文中还提供了一个简化的代码示例,展示了如何在LabVIEW环境中实现这一过程。最后对这项技术进行了总结和展望,强调其在提高数据交互效率方面的潜力以及未来的广泛应用前景。 适合人群:从事工业自动化领域的工程师和技术人员,尤其是那些熟悉LabVIEW或三菱PLC的人士。 使用场景及目标:适用于需要频繁进行数据交互的工业控制系统,如生产线监控、设备状态监测等场合。主要目的是提升数据传输的速度和可靠性,从而优化整个系统的运行效率。 阅读建议:读者可以通过本文深入了解LabVIEW与三菱PLC通信的实现方法,掌握批量数据读写库的设计思路,并将其应用于实际工程项目中。建议边阅读边尝试动手实践相关代码,以便更好地理解和吸收所学知识。
recommend-type

欧姆龙PLC NJ系列模切机程序:高级伺服运动与张力控制的应用实例

欧姆龙PLC NJ系列模切机项目的编程细节及其关键技术。主要内容涵盖12轴EtherCAT总线伺服运动控制,包括回零、点动、定位和速度控制;张力控制采用PID算法并进行收放卷径计算;隔膜自动纠偏控制利用模拟量数据平均化处理;同步运动控制实现凸轮表追剪和裁切;以及结构化编程和ST语言功能块的使用。项目结构规范,注释详尽,有助于理解和维护代码。通过本项目的学习,可以掌握PLC高端复杂应用的实际操作技能。 适合人群:从事工业自动化领域的工程师和技术人员,特别是对PLC编程和伺服运动控制有浓厚兴趣的人群。 使用场景及目标:适用于需要深入了解PLC编程技巧和自动化控制系统原理的技术人员。目标是提升编程能力和对复杂自动化系统的工作机制的理解。 其他说明:本文不仅提供具体的编程指导,还强调了项目管理和代码规范的重要性,为读者提供了全面的学习体验。
recommend-type

Python程序TXLWizard生成TXL文件及转换工具介绍

### 知识点详细说明: #### 1. 图形旋转与TXL向导 图形旋转是图形学领域的一个基本操作,用于改变图形的方向。在本上下文中,TXL向导(TXLWizard)是由Esteban Marin编写的Python程序,它实现了特定的图形旋转功能,主要用于电子束光刻掩模的生成。光刻掩模是半导体制造过程中非常关键的一个环节,它确定了在硅片上沉积材料的精确位置。TXL向导通过生成特定格式的TXL文件来辅助这一过程。 #### 2. TXL文件格式与用途 TXL文件格式是一种基于文本的文件格式,它设计得易于使用,并且可以通过各种脚本语言如Python和Matlab生成。这种格式通常用于电子束光刻中,因为它的文本形式使得它可以通过编程快速创建复杂的掩模设计。TXL文件格式支持引用对象和复制对象数组(如SREF和AREF),这些特性可以用于优化电子束光刻设备的性能。 #### 3. TXLWizard的特性与优势 - **结构化的Python脚本:** TXLWizard 使用结构良好的脚本来创建遮罩,这有助于开发者创建清晰、易于维护的代码。 - **灵活的Python脚本:** 作为Python程序,TXLWizard 可以利用Python语言的灵活性和强大的库集合来编写复杂的掩模生成逻辑。 - **可读性和可重用性:** 生成的掩码代码易于阅读,开发者可以轻松地重用和修改以适应不同的需求。 - **自动标签生成:** TXLWizard 还包括自动为图形对象生成标签的功能,这在管理复杂图形时非常有用。 #### 4. TXL转换器的功能 - **查看.TXL文件:** TXL转换器(TXLConverter)允许用户将TXL文件转换成HTML或SVG格式,这样用户就可以使用任何现代浏览器或矢量图形应用程序来查看文件。 - **缩放和平移:** 转换后的文件支持缩放和平移功能,这使得用户在图形界面中更容易查看细节和整体结构。 - **快速转换:** TXL转换器还提供快速的文件转换功能,以实现有效的蒙版开发工作流程。 #### 5. 应用场景与技术参考 TXLWizard的应用场景主要集中在电子束光刻技术中,特别是用于设计和制作半导体器件时所需的掩模。TXLWizard作为一个向导,不仅提供了生成TXL文件的基础框架,还提供了一种方式来优化掩模设计,提高光刻过程的效率和精度。对于需要进行光刻掩模设计的工程师和研究人员来说,TXLWizard提供了一种有效的方法来实现他们的设计目标。 #### 6. 系统开源特性 标签“系统开源”表明TXLWizard遵循开放源代码的原则,这意味着源代码对所有人开放,允许用户自由地查看、修改和分发软件。开源项目通常拥有活跃的社区,社区成员可以合作改进软件,添加新功能,或帮助解决遇到的问题。这种开放性促进了技术创新,并允许用户根据自己的需求定制软件。 #### 7. 压缩包子文件的文件名称列表 文件名称列表中的“txlwizard-master”可能指的是TXLWizard项目的主版本库或主分支。这个名称表明了这是项目源代码的中心点,其他开发者会从这个主分支拉取代码进行合作开发或部署。以“-master”结尾通常是版本控制系统中表示主要开发线路的常见约定,例如Git中的master(现在更常被称为main)分支。 通过这些知识点的详细解释,我们可以看到TXLWizard不仅是一个用于生成TXL文件的工具,它还整合了一系列的功能,使得电子束光刻掩模的设计工作更为高效和直观。同时,作为一个开源项目,它能够借助社区的力量不断进步,为用户带来更多的便利和创新。
recommend-type

【创新图生成:扣子平台的技术前沿与创新思维】:引领图像生成技术的新潮流

# 1. 扣子平台的图像生成技术概述 在当今数字化时代,图像生成技术正成为推动技术创新和社会变革的关键力量。扣子平台正是这一技术革新的代表。本章将对扣子平台的图像生成技术进行一个全面概述,探究它如何将理论技术转化为实际应用,并带来显著的社会影响。 扣子平台运用了先进的图像生成技术,将现实世界和数字空间无缝融合,打造了沉浸式的视觉体验。其技术的核心是通过高度复杂的算法模型,快速生成高质
recommend-type

海康威视机器视觉工程师考核

<think>好的,我现在需要帮助用户了解海康威视机器视觉工程师的考核内容和要求。根据用户提供的引用资料,特别是引用[1]和[2],里面提到了考核素材包分为初级和中级,涵盖理论、算法、应用案例等。首先,我要整理这些信息,确保结构清晰,符合用户要求的格式。 接下来,我需要确认素材包的具体内容,比如初级和中级的不同点。引用[2]提到初级包含基础理论、算法实现和实际案例,中级则增加复杂算法和项目分析。这部分需要分点说明,方便用户理解层次。 另外,用户可能想知道如何准备考核,比如下载素材、学习顺序、模拟考核等,引用[2]中有使用说明和注意事项,这部分也要涵盖进去。同时要注意提醒用户考核窗口已关闭,
recommend-type

Linux环境下Docker Hub公共容器映像检测工具集

在给出的知识点中,我们需要详细解释有关Docker Hub、公共容器映像、容器编排器以及如何与这些工具交互的详细信息。同时,我们会涵盖Linux系统下的相关操作和工具使用,以及如何在ECS和Kubernetes等容器编排工具中运用这些检测工具。 ### Docker Hub 和公共容器映像 Docker Hub是Docker公司提供的一项服务,它允许用户存储、管理以及分享Docker镜像。Docker镜像可以视为应用程序或服务的“快照”,包含了运行特定软件所需的所有必要文件和配置。公共容器映像指的是那些被标记为公开可见的Docker镜像,任何用户都可以拉取并使用这些镜像。 ### 静态和动态标识工具 静态和动态标识工具在Docker Hub上用于识别和分析公共容器映像。静态标识通常指的是在不运行镜像的情况下分析镜像的元数据和内容,例如检查Dockerfile中的指令、环境变量、端口映射等。动态标识则需要在容器运行时对容器的行为和性能进行监控和分析,如资源使用率、网络通信等。 ### 容器编排器与Docker映像 容器编排器是用于自动化容器部署、管理和扩展的工具。在Docker环境中,容器编排器能够自动化地启动、停止以及管理容器的生命周期。常见的容器编排器包括ECS和Kubernetes。 - **ECS (Elastic Container Service)**:是由亚马逊提供的容器编排服务,支持Docker容器,并提供了一种简单的方式来运行、停止以及管理容器化应用程序。 - **Kubernetes**:是一个开源平台,用于自动化容器化应用程序的部署、扩展和操作。它已经成为容器编排领域的事实标准。 ### 如何使用静态和动态标识工具 要使用这些静态和动态标识工具,首先需要获取并安装它们。从给定信息中了解到,可以通过克隆仓库或下载压缩包并解压到本地系统中。之后,根据需要针对不同的容器编排环境(如Dockerfile、ECS、Kubernetes)编写配置,以集成和使用这些检测工具。 ### Dockerfile中的工具使用 在Dockerfile中使用工具意味着将检测工具的指令嵌入到构建过程中。这可能包括安装检测工具的命令、运行容器扫描的步骤,以及将扫描结果集成到镜像构建流程中,确保只有通过安全和合规检查的容器镜像才能被构建和部署。 ### ECS与Kubernetes中的工具集成 在ECS或Kubernetes环境中,工具的集成可能涉及到创建特定的配置文件、定义服务和部署策略,以及编写脚本或控制器来自动执行检测任务。这样可以在容器编排的过程中实现实时监控,确保容器编排器只使用符合预期的、安全的容器镜像。 ### Linux系统下的操作 在Linux系统下操作这些工具,用户可能需要具备一定的系统管理和配置能力。这包括使用Linux命令行工具、管理文件系统权限、配置网络以及安装和配置软件包等。 ### 总结 综上所述,Docker Hub上的静态和动态标识工具提供了一种方法来检测和分析公共容器映像,确保这些镜像的安全性和可靠性。这些工具在Linux开发环境中尤为重要,因为它们帮助开发人员和运维人员确保他们的容器映像满足安全要求。通过在Dockerfile、ECS和Kubernetes中正确使用这些工具,可以提高应用程序的安全性,减少由于使用不安全的容器镜像带来的风险。此外,掌握Linux系统下的操作技能,可以更好地管理和维护这些工具,确保它们能够有效地发挥作用。
recommend-type

【扣子平台图像艺术探究:理论与实践的完美结合】:深入学习图像生成的艺术

# 1. 图像艺术的理论基础 艺术领域的每一个流派和技巧都有其理论基础。在图像艺术中,理论基础不仅是对艺术表现形式的认知,也是掌握艺术创作内在逻辑的关键。深入理解图像艺术的理论基础,能够帮助艺术家们在创作过程中更加明确地表达自己的艺术意图,以及更好地与观众沟通。 图像艺术的理论
recommend-type

增广路定理的证明

<think>嗯,用户想查找增广路定理的证明,关于图论和网络流算法的。我得先理解增广路定理是什么,然后找到相关的证明方法。根据之前的引用,尤其是引用5提到最大流最小割定理,里面有三个等价条件,其中第二个是残余网络中没有增广路径时,流就是最大流。这可能和增广路定理相关,也就是当残余网络中没有增广路时,当前流就是最大流,这可能就是增广路定理的内容。 首先,我需要明确增广路定理的陈述。根据引用5,增广路定理可能指的是:一个流是最大流当且仅当残余网络中不存在增广路径。这个定理的证明需要用到最大流最小割定理,也就是第三个条件,即最大流的流量等于最小割的容量。 证明的步骤可能需要分为两个方向:必要性(
recommend-type

Pulse:基于SwiftUI的Apple平台高效日志记录与网络监控

从给定文件信息中,我们可以提取出以下IT知识点进行详细阐述: **Pulse概览:** Pulse是一个专门针对Apple平台(如iOS、iPadOS、macOS等)的功能强大的日志记录系统。其设计目的是为了简化开发者在这些平台上调试网络请求和应用日志的过程。Pulse的核心特色是它使用SwiftUI来构建,这有助于开发者利用现代Swift语言的声明式UI优势来快速开发和维护。 **SwiftUI框架:** SwiftUI是一种声明式框架,由苹果公司推出,用于构建用户界面。与传统的UIKit相比,SwiftUI使用更加简洁的代码来描述界面和界面元素,它允许开发者以声明的方式定义视图和界面布局。SwiftUI支持跨平台,这意味着同一套代码可以在不同的Apple设备上运行,大大提高了开发效率和复用性。Pulse选择使用SwiftUI构建,显示了其对现代化、高效率开发的支持。 **Network Inspector功能:** Pulse具备Network Inspector功能,这个功能使得开发者能够在开发iOS应用时,直接从应用内记录和检查网络请求和日志。这种内嵌式的网络诊断能力非常有助于快速定位网络请求中的问题,如不正确的URL、不返回预期响应等。与传统的需要外部工具来抓包和分析的方式相比,这样的内嵌式工具大大减少了调试的复杂性。 **日志记录和隐私保护:** Pulse强调日志是本地记录的,并保证不会离开设备。这种做法对隐私保护至关重要,尤其是考虑到当前数据保护法规如GDPR等的严格要求。因此,Pulse的设计在帮助开发者进行问题诊断的同时,也确保了用户数据的安全性。 **集成和框架支持:** Pulse不仅仅是一个工具,它更是一个框架。它能够记录来自URLSession的事件,这意味着它可以与任何使用URLSession进行网络通信的应用或框架配合使用,包括但不限于Apple官方的网络库。此外,Pulse与使用它的框架(例如Alamofire)也能够良好配合,Alamofire是一个流行的网络请求库,广泛应用于Swift开发中。Pulse提供了一个PulseUI视图组件,开发者可以将其集成到自己的应用中,从而展示网络请求和其他事件。 **跨平台体验:** 开发者不仅可以在iOS应用中使用Pulse Console记录日志,还可以在macOS上通过Pulse应用程序查看和共享这些日志。这种跨平台的能力意味着开发者可以在不同的设备上进行日志分析,增强了开发和调试的灵活性。 **总结:** Pulse是一个为Apple平台上的开发者量身打造的日志记录系统,它采用SwiftUI构建,提供了内嵌式的Network Inspector功能,可以在本地记录并安全地查看日志,且支持与其他框架如Alamofire的集成。它不仅提升了调试的便捷性和效率,同时也顾及到了用户的隐私保护。Pulse的跨平台查看能力也是其一大亮点,使得开发者能够在一个统一的环境中处理iOS和macOS上的日志数据。对于使用Swift开发Apple应用的开发者而言,Pulse无疑是一个强大的调试辅助工具。