SELECT A.THREEBONAME 三级预算中心, A.PRODUCTNAME2 产品大类, A.PRODUCTCODE4 物料编码, A.PRODUCTNAME4 物料名称, A.天数 活动天数, case when A.天数=0 THEN A.差值 ELSE A.天数 END AS 已持续天数, sum(B.POS_QTY_PCS) 活动数量和, sum(B.POS_QTY_PKG) 活动箱数和, sum(B.POS_AMT) 活动金额和 from (SELECT A.ONEBONAME, A.TWOBONAME, A.THREEBONAME, A.PRODUCTNAME2, A.PRODUCTCODE4, A.PRODUCTNAME4, DATE_FORMAT(A.ACTIVITYSTARTDATE,'%Y%m%d') STARTDATE, DATE_FORMAT(A.ACTIVITYENDDATE,'%Y%m%d') ENDDATE, DATE_FORMAT(DATE_SUB(A.ACTIVITYSTARTDATE, INTERVAL 91 DAY),'%Y%m%d') 未促销开始日期, A.ACTIVITYENDDATE-A.ACTIVITYSTARTDATE+1 天数, to_days(now()) - to_days(A.ACTIVITYSTARTDATE)+1 差值 FROM ka_wantwant_prod.VACTIVITYDETAIL A ) A LEFT JOIN (select A.KA_SYSTEM_CODE, B.BW_SYSTEM_NAME, A.SALES_COM_ID_WH, B.KA_SALES_COM_ID_WH_NAME, B.TPM_THREEBONAME, A.PROD_MATL_ID, A.POS_DATE, A.POS_QTY_PCS, A.POS_QTY_PKG, A.POS_AMT FROM ka_wantwant_prod.tras_ka_pos_history A left join (select distinct C.KA_SALES_COM_ID_WH,C.KA_SALES_COM_ID_WH_NAME,C.BW_SYSTEM_CODE,C.BW_SYSTEM_NAME,C.TPM_THREEBONAME from ka_wantwant_prod.ka_pos_tpm_config C) B on A.KA_SYSTEM_CODE=B.BW_SYSTEM_CODE AND A.SALES_COM_ID_WH=B.KA_SALES_COM_ID_WH ) B ON A.PRODUCTCODE4=B.PROD_MATL_ID AND A.THREEBONAME=B.TPM_THREEBONAME AND B.POS_DATE<=A.ENDDATE AND B.POS_DATE>=A.STARTDATE where DATE_FORMAT(A.STARTDATE,'%Y%m')='202305' and B.POS_QTY_PCS is not null GROUP BY A.THREEBONAME, A.PRODUCTNAME2, A.PRODUCTCODE4, A.PRODUCTNAME4, A.天数, 已持续天数
时间: 2024-02-14 12:32:19 浏览: 173
这段 SQL 语句的作用是查询某个时间段内的营销活动和销售数据,并对其进行汇总。其中包括:
- 三级预算中心、产品大类、物料编码、物料名称、活动天数、已持续天数、活动数量和、活动箱数和、活动金额和等字段;
- 使用子查询获取营销活动的相关信息,并计算出活动天数和已持续天数;
- 使用左连接关联销售数据,对数据进行汇总,同时过滤掉 POS_QTY_PCS 为空的记录;
- 使用 GROUP BY 对结果进行分组,以便进行汇总计算。
需要注意的是,这段 SQL 语句在执行时可能存在性能问题,可以考虑进行优化。具体优化方法可以根据实际情况进行调整,例如:
- 创建合适的索引,以加快查询速度;
- 避免在 WHERE 子句中使用函数,以避免索引失效;
- 尽可能使用 INNER JOIN 而不是 LEFT JOIN;
- 避免在 GROUP BY 子句中使用过多的字段,以提高查询效率;
- 避免使用 SELECT *,只选择需要的列等。
相关问题
优化这条sql: select distinct (select product_name from t_product from where id = #{productId} and mark = 1 and status = 1) as productName, (select count(0) from t_clue a where a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId} and a.status in(1,2,3,31,32,33)) as clueCount, (select count(0) from t_clue a left join t_clue_appendix b on a.clue_code = b.clue_code where a.distribution_status != 4 and a.mark = 1 and b.file_url is not null and a.product_id = #{productId} and a.status in (3,31,32,33)) as intentionCount, (select count(0) from t_clue a where a.status in (4,5,7,8) and a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId} and a.status = 4) as incomingCount, (select count(0) from t_clue a where a.status in (5,7,8) and a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId} and a.status in (5,7,8)) as approvedCount, (select count(0) from t_clue a where a.status = 6 and a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId} and a.status = 6) as rejectionCount, (select count(0) from t_clue a where a.status in (7,8) and a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId}) as loanCount, (select count(0) from t_clue a where a.status = 8 and a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId}) as swipeCount, (select sum(a.loan_amount) from t_clue a where a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId}) as loanMoney, (select sum(a.use_amount) from t_clue a where a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId}) as swipeMoney
可以考虑使用 JOIN 语句替换子查询,同时对查询中的字段添加索引以提高查询效率。修改后的 SQL 可能如下所示:
```
SELECT
t1.product_name AS productName,
COUNT(CASE WHEN t2.status IN (1,2,3,31,32,33) THEN 1 END) AS clueCount,
COUNT(CASE WHEN t2.status IN (3,31,32,33) AND t3.file_url IS NOT NULL THEN 1 END) AS intentionCount,
COUNT(CASE WHEN t2.status = 4 THEN 1 END) AS incomingCount,
COUNT(CASE WHEN t2.status IN (5,7,8) THEN 1 END) AS approvedCount,
COUNT(CASE WHEN t2.status = 6 THEN 1 END) AS rejectionCount,
COUNT(CASE WHEN t2.status IN (7,8) THEN 1 END) AS loanCount,
COUNT(CASE WHEN t2.status = 8 THEN 1 END) AS swipeCount,
SUM(t2.loan_amount) AS loanMoney,
SUM(t2.use_amount) AS swipeMoney
FROM
t_product t1
JOIN t_clue t2 ON t1.id = t2.product_id
LEFT JOIN t_clue_appendix t3 ON t2.clue_code = t3.clue_code
WHERE
t1.id = #{productId} AND t1.mark = 1 AND t1.status = 1 AND t2.distribution_status != 4 AND t2.mark = 1
GROUP BY
t1.product_name;
```
function getPagedList(contractId) { var contractId = body.param.ontractId; SELECT a.id, a.conract_id contractId, a.product_id productId, a.purchase_num purchaseNum, a.purchase_price purchasePrice, a.remark, b.product_name productName, b.spec_code specCode, c.type_name typeName, d.unit_code unitCode FROM mes214820519.wms_pur_list_214820519 a, mes214820519.wms_product_214820519 b, mes214820519.wms_product_class_214820519 c, mes214820519.wms_unit_code_214820519 d WHERE a.product_id=b.id AND b.product_class_id = c.id AND b.unit_code_id=d.id `; return db.pager(sql, {contractId: contractId}) } 这段代码的错误在哪
这段代码有两个错误:
1. 在第一行中声明了 `contractId`,但在 SQL 查询语句中使用了 `body.param.ontractId`,应该改为 `body.param.contractId`。
2. SQL 查询语句的末尾多了一个反引号,应该删除它。
正确的代码应该是:
```
function getPagedList(contractId) {
var contractId = body.param.contractId;
var sql = `
SELECT
a.id,
a.conract_id contractId,
a.product_id productId,
a.purchase_num purchaseNum,
a.purchase_price purchasePrice,
a.remark,
b.product_name productName,
b.spec_code specCode,
c.type_name typeName,
d.unit_code unitCode
FROM
mes214820519.wms_pur_list_214820519 a,
mes214820519.wms_product_214820519 b,
mes214820519.wms_product_class_214820519 c,
mes214820519.wms_unit_code_214820519 d
WHERE
a.product_id=b.id
AND b.product_class_id = c.id
AND b.unit_code_id=d.id`;
return db.pager(sql, {contractId: contractId})
}
```
阅读全文