Calculating margin through mysql query - mysql

My mysql query is as below which give error
Below is with if else
SELECT
o.id,
p.product_name,
p.admin_price,
co.product_amt_disc,
(co.product_amt_disc + product_vat_disc) AS sale_amount,
(IF co.retake_flag = 1 THEN
((co.product_amt_disc - (p.admin_price+6))/(co.product_amt_disc))
ELSE
((co.product_amt_disc - p.admin_price)/(co.product_amt_disc))
END IF
) as margin FROM
customer_orders co
LEFT JOIN
m_products p ON p.id = co.product_id
LEFT JOIN
orders o on o.id=co.order_id;
Below on is with case
SELECT
o.id,
p.product_name,
p.admin_price,
co.product_amt_disc,
(co.product_amt_disc + product_vat_disc) AS sale_amount,
(CASE WHEN co.retake_flag = 1 THEN
((co.product_amt_disc - (p.admin_price+6))/(co.product_amt_disc))
ELSE
((co.product_amt_disc - p.admin_price)/(co.product_amt_disc))
) as margin FROM
customer_orders co
LEFT JOIN
m_products p ON p.id = co.product_id
LEFT JOIN
orders o on o.id=co.order_id;
I am getting below error for id
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'co.retake_flag = 1 THEN ((co.product_amt_disc - (p.admin_price+6))/(co.product' at line 7
I am getting error for CASE
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') as margin FROM customer_orders co LEFT JOIN m_products p ON p.' at line 11
I want to calculate percentage on behalf of condition retake flag. please help and let me know if anything else required
Thanks

I was missing END in CASE statement
the correct answer is as below
SELECT
o.id,
p.product_name,
p.admin_price,
co.product_amt_disc,
(co.product_amt_disc + product_vat_disc) AS sale_amount,
(CASE WHEN co.retake_flag = 1 THEN
((co.product_amt_disc - (p.admin_price+6))/(co.product_amt_disc))
ELSE
((co.product_amt_disc - p.admin_price)/(co.product_amt_disc) ) END
) as margin
FROM
customer_orders co
LEFT JOIN
m_products p ON p.id = co.product_id
LEFT JOIN
orders o on o.id=co.order_id;

Related

ER_PARSE_ERROR don't know how to fix

select Customer.CustNo, Customer.FirstName, Customer.LastName,
`Order`.OrderNo, `Order`.OrderDate, Employee.EmpNo,
Employee.FirstName, Employee,LastName, Product.ProdNo,
Product.ProdName,
CAST(substring(Product.Price,2) AS FLOAT) * ProductInOrder.Qty AS 'OrderCost'
From `Order`
inner join Employee on `Order`.Employee_EmpNo = Employee.EmpNo
inner join Customer on Customer.CustNo = `Order`Customer_CustNo
inner join ProductInOrder on `Order`.OrderNo = ProductInOder.Order_OrderNo
inner join Product on ProductInOrder.Product_ProdNo = Product.ProductNo
Where Employee.EmpNo is not null
and str_to_date(`Order`.OrderDate,'%Y-%m-%d') = '2007-1-23'
Having OrderCost > 150
Order by OrderCost Desc
After I execute it the 'ER_PARSE_ERROR' error appear.
Full error message:
ER_PARSE_ERROR
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FLOAT) * ProductInOrder.Qty AS 'OrderCost' From Order inner join Employee on `' at line 1'

How do I fix my code so it runs correctly, it keeps telling me I have Error Code 1064

Trying to complete this multi-query assignment but cannot figure out why I keep getting this error code (Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY E.EventDate ASC' at line 3). Below is my code
SELECT C.ClientID, C.ClientName, EC.ClientID, EC.EventCode, E.EventCode, E.EventName, E.EventDate, NT.EventCode, T.EventCode
FROM Non_Ticketed_Events AS NT
LEFT JOIN Events AS E
LEFT JOIN Ticketed_Events AS T
LEFT JOIN Client AS C
LEFT JOIN Event_Contact AS EC
ON NT.EventCode = E.EventCode
AND T.EventCode = E.EventCode
AND E.EventCode = EC.EventCode
AND EC.ClientID = C.ClientID
ORDER BY E.EventDate ASC;
Each join condition must be place to the specific ON clause
SELECT C.ClientID
, C.ClientName
, EC.ClientID
, EC.EventCode
, E.EventCode
, E.EventName
, E.EventDate
, NT.EventCode
, T.EventCode
FROM Non_Ticketed_Events NT
LEFT JOIN Events E ON NT.EventCode = E.EventCode
LEFT JOIN Ticketed_Events T ON T.EventCode = E.EventCode
LEFT JOIN Client C ON EC.ClientID = C.ClientID
LEFT JOIN Event_Contact EC ON E.EventCode = EC.EventCode
ORDER BY E.EventDate ASC;

Incorrect CASE syntax

Here’s my query:
SELECT
CASE WHEN COUNT(o) > 0 THEN true ELSE false
FROM orders o
left join shop_order so on o.id=so.order_id
left join order_details od on so.id=od.shop_order_id
left join offers of on od.offer_id=of.id
WHERE of.offer_type_id=1
and of.type=2
and o.customer_id = ?1
and od.varient_id=?2
the error is :
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM orders o left join shop_order so on o.id=so.order_id left join order_det
The case is missing the end keyword. Insert END before FROM:
SELECT
CASE WHEN COUNT(o) > 0 THEN true ELSE false END
FROM ...
MySQL error messages say “near”, but they really mean “right before”.

What is the correct syntax?

I wrote a sql query to update data of 'tbl_products'.
Here is the query
update tbl_products
set product_count = (product_count - tbl_order_details.product_sales_quantity)
from tbl_products
join tbl_order_details on tbl_order_details.product_id = tbl_products.product_id
join tbl_order on tbl_order.order_id = tbl_order_details.order_id
where tbl_order.order_id = 54;
But it gives me the following error
"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from tbl_products join tbl_order_details on tbl_order_details.product_id = tbl_p' at line 1"
Whats wrong here?
In MySQL, the correct syntax is:
update tbl_products p join
tbl_order_details od
on od.product_id = p.product_id join
tbl_order o
on o.order_id = od.order_id
set p.product_count = (p.product_count - od.product_sales_quantity)
where o.order_id = 54;

MYSQL Error Code: 1064 in the view

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT OUTER JOIN dw_fact_claim ON dw_fact_claim.patient_id = dw_snf.patient_id
' at line 13
my query is:
CREATE VIEW DW_SNF
AS
SELECT
REL ,
PATIENT_ID ,
MIN( CLM_FROM_DT )AS clm_from_dt ,
MAX( clm_THRU_dt )AS clm_THRU_dT
FROM dw_SNF
WHERE REL <> ''
GROUP BY
REL ,
PATIENT_ID
LEFT OUTER JOIN dw_fact_claim ON dw_fact_claim.patient_id = dw_snf.patient_id
AND dw_fact_claim.CLM_FROM_DT >= dw_SNF.clm_from_dt
AND dw_fact_claim.CLM_THRU_DT <= dw_SNF.clm_thru_dt
AND dw_fact_claim.TYPE_OF_CLAIM NOT LIKE '%snf%'
WHERE dw_SNF.rel <> ''
GROUP BY
rel ON SUMMARY.REL = SNF_BASE.REL
INNER JOIN dw_VIEW_dim_patient_FLAG AS DW_DIM_PATIENT ON dw_dim_patient.patient_id = dw_snf.patient_id
INNER JOIN DW_VIEW_JH_ACG_CONDENSED dw_jh_acg ON dw_jh_acg.patient_id = dw_dim_patient.patient_id
WHERE SNF_EPISODE_ID <> '';
I have used where clause before left outer join,that's the error was and after using where condition at the end of join conditions i have created view successfully.We can not have JOIN in Group by clause.