samedi 11 juin 2016

How can I code these 2 sql queries into 1 to get the results I need

I have a complicated query that produces almost everything I need to see:

SELECT suppliers_prods.reference,products.id,products.price,products.current,suppliers_prods.price,
suppliers_prods.costoverhead,stock.total,stock.location,products.maxstock,manufacturers.name,products.title,products.reference,products.weight,suppliers.name,products.manufacturerref,stock.location,suppliers_prods.adjusted_overhead,
suppliers_prods.1_of_trade_perc,suppliers_prods.more_than,suppliers_prods.multiple_of_trade_perc,
suppliers_prods.last_order_amount,DATE_FORMAT(suppliers_prods.last_ordered,"%d-%m-%Y %T") AS supplier_last_ordered,prodtypes.description AS prodtype, DATE_FORMAT(MAX(orders.orderdate),"%d-%m-%Y %T") AS item_last_ordered, SUM(wishlist.qty) AS notify_waiting_qty

FROM products

INNER JOIN prodtypes ON products.prodtype=prodtypes.id

INNER JOIN stock ON products.id=stock.prodid

INNER JOIN suppliers_prods ON suppliers_prods.prodid=products.id

INNER JOIN suppliers ON suppliers_prods.supplierid=suppliers.id

INNER JOIN manufacturers ON products.manufacturer=manufacturers.id

LEFT JOIN wishlist on products.id=wishlist.prodid

LEFT JOIN order_items ON products.id=order_items.itemid

LEFT JOIN orders ON order_items.orderid=orders.id

WHERE products.current = "1" AND suppliers.name = "ICS"

GROUP BY products.id

ORDER BY products.current,stock.total;

The problem is that the field notify_waiting_qty has the wrong data in it.

I can run the following query:

select suppliers_prods.reference,wishlist.prodid,count(DISTINCT wishlist.custid) as qty
from wishlist
INNER JOIN products ON products.id=wishlist.prodid
INNER JOIN suppliers_prods ON suppliers_prods.prodid=products.id
where 
DATE_ADD(wishlist.date_added,INTERVAL 2 YEAR) >= CURDATE() and 
wishlist.notified = "0"
group by wishlist.prodid

This provides me the field QTY which contains the correct information for the field suppliers_prods.reference.

The only way I can then combine the data is in Excel: I paste the results from query 1 into sheet 1 of Excel and then query 2 into sheet 2.

Then on the sheet 1 column notify_waiting_qty using the field suppliers_prods.reference I do a lookup of sheet2 and find the value for the QTY and put that into sheet

So result from SQL query 1 is (it's a copy of the csv file):

suppliers_prods.reference,products.id,products.price,products.current,suppliers_prods.price,suppliers_prods.costoverhead,stock.total,stock.location,products.maxstock,manufacturers.name,products.title,products.reference,products.weight,suppliers.name,products.manufacturerref,stock.location,suppliers_prods.adjusted_overhead,suppliers_prods.1_of_trade_perc,suppliers_prods.more_than,suppliers_prods.multiple_of_trade_perc,suppliers_prods.last_order_amount,.supplier_last_ordered,prodtypes.prodtype,.item_last_ordered,.notify_waiting_qty
MA-09,2686,15,1,3.11,1.17,0,M1-R3-S4-P2-D1,15,ICS,ICS M4 Hop Up Unit,ICS-MA-09,99,ICS,MA-09,M1-R3-S4-P2-D1,1.17,5.56,1,5.56,15,31/03/2016 15:28,Hopup,15/05/2016 18:40,16378

Result of query 2:

suppliers_prods.reference,wishlist.prodid,.qty
MA-09,2686,2

So in my Excel spreadsheet and using Excel commands I can get the MA-09 QTY value of 2 into the MA-09 row field notify_waiting_qty, resulting in a row containing all the data I need to see:

suppliers_prods.reference,products.id,products.price,products.current,suppliers_prods.price,suppliers_prods.costoverhead,stock.total,stock.location,products.maxstock,manufacturers.name,products.title,products.reference,products.weight,suppliers.name,products.manufacturerref,stock.location,suppliers_prods.adjusted_overhead,suppliers_prods.1_of_trade_perc,suppliers_prods.more_than,suppliers_prods.multiple_of_trade_perc,suppliers_prods.last_order_amount,.supplier_last_ordered,prodtypes.prodtype,.item_last_ordered,.notify_waiting_qty
MA-09,2686,15,1,3.11,1.17,0,M1-R3-S4-P2-D1,15,ICS,ICS M4 Hop Up Unit,ICS-MA-09,99,ICS,MA-09,M1-R3-S4-P2-D1,1.17,5.56,1,5.56,15,31/03/2016 15:28,Hopup,15/05/2016 18:40,2

The Excel statement I use to get the data from sheet 2 to sheet 1 is:

=IF(ISNA(VLOOKUP(A2, Sheet2!$A$1:$O$2999,3, FALSE)),0,VLOOKUP(A2, Sheet2!$A$1:$O$2999,3, FALSE))

Is what I am asking for possible? If yes, how do I code this I think Union would give me 2 rows of data for the same suppliers_prods.reference but I only want one row of data for each suppliers_prods.reference.

The reason for the complicated code on query 1 is that I need the latest order date value and this code gets me this.

Aucun commentaire:

Enregistrer un commentaire