ZOHO Analytics下销售佣金计算SQL多子查询重构为单级子查询方案
ZOHO Analytics 销售佣金计算SQL重构问题
ZOHO Analytics是一款支持MySQL、SQL Server、Oracle、PostgreSQL等多类SQL语法的数据库,本次开发使用MySQL语法。
原本编写的销售佣金计算完整SQL包含4层嵌套子查询,但ZOHO Analytics平台仅允许最多1层子查询,因此此前将完整SQL拆分为2个视图表实现。
*注:双引号包裹的文本为表名或字段名。
初始拆分实现
视图表1:Sales Person Commission Part 1
select sp."name" as "Sales Person Name", round(t1."gross sales", 2) as "Gross Sales", if(t1."Gross Partner Profit" is null, 0.00, round(t1."Gross Partner Profit", 2)) as "Gross Partner Profit", round(t1."Gross Sales" - if(t1."Gross Partner Profit" is null, 0, t1."Gross Partner Profit"), 2) as "Gross Profit", round(100 * (t1."Gross Sales" - if(t1."Gross Partner Profit" is null, 0, t1."Gross Partner Profit")) / t1."gross sales", 2) as "Gross Profit Percentage" from ( select i."sales person id", sum(ii."total (bcy)") as "gross sales", sum(item."partner price" * ii."quantity") as "Gross Partner Profit" from "invoice items (zoho finance)" ii inner join "invoices (zoho finance)" i on i."invoice id"=ii."invoice id" left join "items (zoho finance)" item on item."item id"=ii."item id" where i."invoice date">='2021-07-01' and i."invoice date"<='2021-07-31' group by i."sales person id" ) as t1 inner join "sales persons (zoho finance)" sp on t1."i.sales person id"=sp."sales person id";
视图表2:基于视图表1计算佣金
select t1.*, round(t1."Commission Percentage" * t1."Gross Sales" / 100, 2) as "Commission Total" from ( select *, case when "Gross Profit Percentage" <= 5 then 0 when "Gross Profit Percentage" > 5 and "Gross Profit Percentage" <= 10 then 1.9 when "Gross Profit Percentage" > 10 and "Gross Profit Percentage" <= 15 then 2.9 when "Gross Profit Percentage" > 15 and "Gross Profit Percentage" <= 20 then 3.9 when "Gross Profit Percentage" > 20 and "Gross Profit Percentage" <= 25 then 4.9 when "Gross Profit Percentage" > 25 and "Gross Profit Percentage" <=30 then 5.9 when "Gross Profit Percentage" > 30 then 7.9 end as "Commission Percentage" from "Sales Person Commission Part 1" ) as t1;
问题诉求
希望将上述两部分逻辑重构为最多仅含1层子查询的单条SQL语句,是否有可行方案?
更新:问题已解决
感谢@shadow提供的思路,最终重构后的SQL如下:
select t1.*, round(t1."Commission Percentage" * t1."Gross Sales" / 100, 2) as "Commission Total" from ( select sp."name" as "Sales Person", round(sum(ii."total (bcy)"), 2) as "Gross Sales", round(if (item."partner price" is null, 0, sum(item."partner price" * ii."quantity")), 2) as "Gross Partner Profit", round(sum(ii."total (bcy)") - if(item."partner price" is null, 0, sum(item."partner price" * ii."quantity")), 2) as "Gross Profit", round(100 * round(sum(ii."total (bcy)") - if(item."partner price" is null, 0, sum(item."partner price" * ii."quantity")), 2) / round(sum(ii."total (bcy)"), 2) , 2) as "Gross Profit Percentage", case when item."partner price" is null then 7.9 when 100 * round(sum(ii."total (bcy)") - if(item."partner price" is null, 0, sum(item."partner price" * ii."quantity")), 2) / round(sum(ii."total (bcy)"), 2) <= 5 then 0 when 100 * round(sum(ii."total (bcy)") - if(item."partner price" is null, 0, sum(item."partner price" * ii."quantity")), 2) / round(sum(ii."total (bcy)"), 2) > 5 and 100 * round(sum(ii."total (bcy)") - if(item."partner price" is null, 0, sum(item."partner price" * ii."quantity")), 2) / round(sum(ii."total (bcy)"), 2) <= 10 then 1.9 when 100 * round(sum(ii."total (bcy)") - if(item."partner price" is null, 0, sum(item."partner price" * ii."quantity")), 2) / round(sum(ii."total (bcy)"), 2) > 10 and 100 * round(sum(ii."total (bcy)") - if(item."partner price" is null, 0, sum(item."partner price" * ii."quantity")), 2) / round(sum(ii."total (bcy)"), 2) <= 15 then 2.9 when 100 * round(sum(ii."total (bcy)") - if(item."partner price" is null, 0, sum(item."partner price" * ii."quantity")), 2) / round(sum(ii."total (bcy)"), 2) > 15 and 100 * round(sum(ii."total (bcy)") - if(item."partner price" is null, 0, sum(item."partner price" * ii."quantity")), 2) / round(sum(ii."total (bcy)"), 2) <= 20 then 3.9 when 100 * round(sum(ii."total (bcy)") - if(item."partner price" is null, 0, sum(item."partner price" * ii."quantity")), 2) / round(sum(ii."total (bcy)"), 2) > 20 and 100 * round(sum(ii."total (bcy)") - if(item."partner price" is null, 0, sum(item."partner price" * ii."quantity")), 2) / round(sum(ii."total (bcy)"), 2) <= 25 then 4.9 when 100 * round(sum(ii."total (bcy)") - if(item."partner price" is null, 0, sum(item."partner price" * ii."quantity")), 2) / round(sum(ii."total (bcy)"), 2) > 25 and 100 * round(sum(ii."total (bcy)") - if(item."partner price" is null, 0, sum(item."partner price" * ii."quantity")), 2) / round(sum(ii."total (bcy)"), 2) <=30 then 5.9 when 100 * round(sum(ii."total (bcy)") - if(item."partner price" is null, 0, sum(item."partner price" * ii."quantity")), 2) / round(sum(ii."total (bcy)"), 2) > 30 then 7.9 end as "Commission Percentage" from "invoice items (zoho finance)" ii inner join "invoices (zoho finance)" i on i."invoice id"=ii."invoice id" inner join "sales persons (zoho finance)" sp on sp."sales person id" = i."sales person id" left join "items (zoho finance)" item on item."item id"=ii."item id" where i."invoice date">='2021-07-01' and i."invoice date"<='2021-07-31' group by i."sales person id",sp."name", item."partner price" order by sp."name" ) as t1;
更新2:空值判断优化版本
使用coalesce(field,value)替换if空值判断的优化版本SQL如下:
select t1.*, round(t1."Commission Percentage" * t1."Gross Sales" / 100, 2) as "Commission Total" from ( select sp."name" as "Sales Person", round(sum(ii."total (bcy)"), 2) as "Gross Sales", round(coalesce(sum(item."partner price" * ii."quantity"), 0)) as "Gross Partner Profit", round(sum(ii."total (bcy)"), 2) - round(coalesce(sum(item."partner price" * ii."quantity"), 0), 2) as "Gross Profit", round(100 * (round(sum(ii."total (bcy)"), 2) - round(coalesce(sum(item."partner price" * ii."quantity"), 0), 2)) / round(sum(ii."total (bcy)"), 2) , 2) as "Gross Profit Percentage", case when item."partner price" is null then 7.9 when 100 * (round(sum(ii."total (bcy)"), 2) - round(coalesce(sum(item."partner price" * ii."quantity"), 0), 2)) / round(sum(ii."total (bcy)"), 2) <= 5 then 0 when 100 * (round(sum(ii."total (bcy)"), 2) - round(coalesce(sum(item."partner price" * ii."quantity"), 0), 2)) / round(sum(ii."total (bcy)"), 2) > 5 and 100 * (round(sum(ii."total (bcy)"), 2) - round(coalesce(sum(item."partner price" * ii."quantity"), 0), 2)) / round(sum(ii."total (bcy)"), 2) <= 10 then 1.9 when 100 * (round(sum(ii."total (bcy)"), 2) - round(coalesce(sum(item."partner price" * ii."quantity"), 0), 2)) / round(sum(ii."total (bcy)"), 2) > 10 and 100 * (round(sum(ii."total (bcy)"), 2) - round(coalesce(sum(item."partner price" * ii."quantity"), 0), 2)) / round(sum(ii."total (bcy)"), 2) <= 15 then 2.9 when 100 * (round(sum(ii."total (bcy)"), 2) - round(coalesce(sum(item."partner price" * ii."quantity"), 0), 2)) / round(sum(ii."total (bcy)"), 2) > 15 and 100 * (round(sum(ii."total (bcy)"), 2) - round(coalesce(sum(item."partner price" * ii."quantity"), 0), 2)) / round(sum(ii."total (bcy)"), 2) <= 20 then 3.9 when 100 * (round(sum(ii."total (bcy)"), 2) - round(coalesce(sum(item."partner price" * ii."quantity"), 0), 2)) / round(sum(ii."total (bcy)"), 2) > 20 and 100 * (round(sum(ii."total (bcy)"), 2) - round(coalesce(sum(item."partner price" * ii."quantity"), 0), 2)) / round(sum(ii."total (bcy)"), 2) <= 25 then 4.9 when 100 * (round(sum(ii."total (bcy)"), 2) - round(coalesce(sum(item."partner price" * ii."quantity"), 0), 2)) / round(sum(ii."total (bcy)"), 2) > 25 and 100 * (round(sum(ii."total (bcy)"), 2) - round(coalesce(sum(item."partner price" * ii."quantity"), 0), 2)) / round(sum(ii."total (bcy)"), 2) <=30 then 5.9 when 100 * (round(sum(ii."total (bcy)"), 2) - round(coalesce(sum(item."partner price" * ii."quantity"), 0), 2)) / round(sum(ii."total (bcy)"), 2) > 30 then 7.9 end as "Commission Percentage" from "invoice items (zoho finance)" ii inner join "invoices (zoho finance)" i on i."invoice id"=ii."invoice id" inner join "sales persons (zoho finance)" sp on sp."sales person id" = i."sales person id" left join "items (zoho finance)" item on item."item id"=ii."item id" where i."invoice date">='2021-07-01' and i."invoice date"<='2021-07-31' group by i."sales person id",sp."name", item."partner price" ) as t1 order by t1."Sales Person"
内容的提问来源于stack exchange,提问作者mjb
相关产品推荐
相关产品推荐

