Andrejkov Posted April 26, 2022 Share Posted April 26, 2022 Hello, I need help with writing a SQL query wwhich I will be able to extract from the database information on the discount codes used, but in a summed form, i.e. how much a given code has been used and what is the total sum of values of orders. Query: SELECT * FROM `ps_order_cart_rule` returns to me the entire contents of the table, now I need to get the discount code "Your first buy" from this data and return how many times this code has been used and what is the total value of orders using this code. Thank you very much in advance for your help. Link to comment Share on other sites More sharing options...
abdullacm Posted April 27, 2022 Share Posted April 27, 2022 (edited) SELECT name, COUNT(*) AS total_used, SUM(value) AS total_value FROM ps_order_cart_rule GROUP BY name try this sql Edited April 27, 2022 by abdullacm added sum of value column (see edit history) 1 Link to comment Share on other sites More sharing options...
Andrejkov Posted April 27, 2022 Author Share Posted April 27, 2022 1 hour ago, abdullacm said: SELECT name, COUNT(*) AS total_used, SUM(value) AS total_value FROM ps_order_cart_rule GROUP BY name try this sql Its work perfect. Thank You. 1 Link to comment Share on other sites More sharing options...
Andrejkov Posted April 27, 2022 Author Share Posted April 27, 2022 2 hours ago, abdullacm said: SELECT name, COUNT(*) AS total_used, SUM(value) AS total_value FROM ps_order_cart_rule GROUP BY name try this sql There is other problem with this query in column "value" i have only discount value but I need to compare this data with orders value. How i Can modify this query to get a orders value from "id_order". Link to comment Share on other sites More sharing options...
abdullacm Posted April 27, 2022 Share Posted April 27, 2022 SELECT c.name, COUNT(*) AS total_used, SUM(o.total_paid) AS order_total FROM ps_order_cart_rule c INNER JOIN ps_orders o ON(o.id_order = c.id_order) GROUP BY c.name 1 Link to comment Share on other sites More sharing options...
Andrejkov Posted April 27, 2022 Author Share Posted April 27, 2022 This is it! Thank you for yoru help 🙂 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now