ZOHO Analytics is a database which supports SQL syntax of MySQL, SQL server, Oracle, PostgreSQL, etc...
I'm using syntax of MySQL.
I am able to construct the whole SQL, it consists of 4 levels of sub-query, but here's the problem. ZOHO Analytics only allows 1 level of sub-query. So, I end up have to split the full SQL into 2 "View Table". Site
*Note: text wraps with double quote refers to table name or column name.
Here's the 1st View Table "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";
Here's the 2nd View Table "Sales Person Commission Part 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;
Is there a possibility to reconstruct both parts into single SQL with maximum only 1 sub-query?