Verngren Posted April 9, 2013 Share Posted April 9, 2013 Hi. Could someone please help me in getting the following code to work: SELECT "all address fields" TOP 500 SORT BY order amount FROM customers WHERE orderstatus = "Delivered" I hope you can see what i want to do without being able to write proper SQL Thanks! // David Link to comment Share on other sites More sharing options...
NemoPS Posted April 10, 2013 Share Posted April 10, 2013 Hi, uhm.. what do you mean by top 500? You only want 500 entries? Link to comment Share on other sites More sharing options...
Verngren Posted April 10, 2013 Author Share Posted April 10, 2013 Yes, thats correct. I want the name and addresses of the people who made the 500 largest purchases. Link to comment Share on other sites More sharing options...
NemoPS Posted April 10, 2013 Share Posted April 10, 2013 SELECT ad.*, SUM(o.total_paid_real) as paid_by_customer FROM ps_address ad LEFT JOIN ps_orders o ON (o.id_customer = ad.id_customer) LEFT JOIN ps_order_state os ON (os.id_order state = o.current_state) WHERE os.name = "Delivered" AND os.id_lang = *enter the language ID here, how it's retrieved varies depending on the place you query from) GROUP BY ad.id_customer ORDER BY paid_by_customer LIMIT 500 It *should* work but it's late and I'm pretty sleepy I haven't tested it either, let me know 1 Link to comment Share on other sites More sharing options...
Verngren Posted April 10, 2013 Author Share Posted April 10, 2013 Right.. so heres what Ive got: SELECT ad.*, SUM(o.total_paid_real) as paid_by_customer FROM ps_address ad LEFT JOIN ps_orders o ON (o.id_customer = ad.id_customer) LEFT JOIN ps_order_state os ON (os.id_order_state = o.current_state) WHERE os.name = "Delivered" AND os.id_lang = 7 GROUP BY ad.id_customer ORDER BY paid_by_customer LIMIT 500 The errors i get are: os.name is not recognized. Checked the table, and there is no column named "name" I also couldnt locate anywhere in the order table that would define the order as delivered. Not by ID nor name. Only history.. I dont know if that should be it. Link to comment Share on other sites More sharing options...
tomerg3 Posted April 11, 2013 Share Posted April 11, 2013 Try this, replace xx with the status ID of "delivered" SELECT a.*, SUM(o.total_paid_real) as total FROM ps_orders o LEFT JOIN ps_address a ON a.id_customer = o.id_customer WHERE o.current_state = xx GROUP BY a.id_address ORDER BY total LIMIT 500 Link to comment Share on other sites More sharing options...
Verngren Posted April 12, 2013 Author Share Posted April 12, 2013 Hi tomerg3.. It gives me the following error: - Unknown column 'o.current_state' in 'where clause' Link to comment Share on other sites More sharing options...
NemoPS Posted April 12, 2013 Share Posted April 12, 2013 This makes me wonder: which is your prestashop version? Link to comment Share on other sites More sharing options...
Verngren Posted April 12, 2013 Author Share Posted April 12, 2013 Prestashop version: 1.4.5.1 Link to comment Share on other sites More sharing options...
NemoPS Posted April 12, 2013 Share Posted April 12, 2013 Oh well, of course it's not working then, try id_order_state instead of current_state Link to comment Share on other sites More sharing options...
Verngren Posted April 12, 2013 Author Share Posted April 12, 2013 SELECT a.*, SUM(o.total_paid_real) as total FROM ps_orders o LEFT JOIN ps_address a ON a.id_customer = o.id_customer WHERE o.id_order_state = 5 GROUP BY a.id_address ORDER BY total LIMIT 500 #1054 - Unknown column 'o.id_order_state' in 'where clause' Link to comment Share on other sites More sharing options...
NemoPS Posted April 13, 2013 Share Posted April 13, 2013 Gosh, i looked for it but that was nowhere to bbe found :/ I fear they managed it through order history at this point :/ Link to comment Share on other sites More sharing options...
musicmaster Posted April 13, 2013 Share Posted April 13, 2013 This should work: SELECT ad.*, SUM(o.total_paid_real) as paid_by_customer FROM ps_address ad LEFT JOIN ps_orders o ON (o.id_customer = ad.id_customer) LEFT JOIN ps_order_history h ON (h.id_order = o.id_order) WHERE h.id_order_state = '4' GROUP BY ad.id_customer ORDER BY paid_by_customer DESC LIMIT 500 One thing to watch is whether you want to order state 4 (= sent) or 5(=delivered). Another things is that this doesn't check for the last status but just checks whether the order once had that status. So the order will still be counted if the products were sent back. Link to comment Share on other sites More sharing options...
Recommended Posts