简体   繁体   中英

SQL sub-query involving calculations that involves multiple tables

can you all show me how to convert the SQL query below into sub-query? I have attempted it multiple times and I still can't find the solution to it. Thank you.

SELECT  
FARMER.Farmer_name AS "Farmer's Name",  
SUM((PRODUCE.Product_price * 0.5) * CART.Quantity * 15/100) AS "Profit Loss" 
FROM  
CART, PRODUCE, FARMER, PAYMENT, PURCHASE 
WHERE
PURCHASE.Order_date BETWEEN TO_DATE ('06/09/2020', 'DD/MM/YYYY') AND TO_DATE ('08/09/2020', 'DD/MM/YYYY') AND 
PAYMENT.Payment_status = 'Paid' AND 
CART.Order_id = PURCHASE.Order_id AND 
CART.Order_id = PAYMENT.Order_id AND 
PRODUCE.Farmer_id = FARMER.Farmer_id AND  
PRODUCE.Product_code = CART.Product_code AND 
PAYMENT.Order_id = PURCHASE.Order_id 
GROUP BY  
FARMER.Farmer_name 
ORDER BY  
"Profit Loss" DESC; 

if you want to create a subquery with the return of the current query is much cleaner if you use a view like:

CREATE VIEW view_name AS
//content of your query

and then calling the view like

SELECT * FROM view_name; //or any selection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM