简体   繁体   中英

Request SQL : Join Table and don't want multiple value

I'm stuck on an SQL query. The cause is since I added a table with a Left JOIN :

This my query, the issue is with Amount_tax_excl

SELECT [...]
  os.amount + os.shipping_cost_amount AS `amount`,
  (CASE 
    WHEN od.id_customization!=0 AND s.id_shop=3 THEN SUM(CAST(os.amount / 1.19 AS decimal(10,6)))
    WHEN od.id_customization!=0 AND s.id_shop=6 THEN SUM(osd.amount_tax_excl)
    WHEN od.id_customization!=0 AND s.id_shop!=3 THEN SUM(CAST(os.amount / 1.2 AS decimal(10,6)))
    ELSE SUM(osd.amount_tax_excl)
  END) AS amount_tax_excl,
  SUM(osd.amount_tax_incl) AS amount_tax_incl,
  os.shipping_cost_amount, os.partial,
  [...]
FROM  ps5_order_slip os
LEFT JOIN ps5_orders o ON (os.id_order = o.id_order)
LEFT JOIN ps5_order_detail od ON (o.id_order = od.id_order AND os.id_order = od.id_order)
LEFT JOIN ps5_shop s ON (o.id_shop = s.id_shop)
LEFT JOIN ps5_address a ON (o.id_address_delivery = a.id_address)
LEFT JOIN ps5_state e ON (a.id_state = e.id_state)
LEFT JOIN ps5_order_slip_detail osd ON (os.id_order_slip = osd.id_order_slip)
WHERE os.date_add >= '2019-07-01' AND os.date_add <= '2019-07-30 23:59:59'
GROUP BY os.id_order_slip

Normally, the result should be 645.35 like amount

https://imgur.com/fTWn7yS

I noticed that it's because of the order_detail table because there are 6 products.

So 645.35 * 6 = 3872.100000. I use DISTINCT, IF ELSEIF, nothing works I really need this variable od.id_customization for this query and it is that in this table.

https://imgur.com/hhVxrmR

(Sorry I don't know how to display the images directly on the stack)

Thanks for help

You are joining to order_detail table using only a header ID. You need to join using line level ID.

I found a way to do it but it's pretty dirty I think.

SELECT [...],
os.amount + os.shipping_cost_amount AS `amount`,
(CASE 
WHEN od.id_customization!=0 AND s.id_shop=3 THEN SUM(CAST(os.amount / 1.19 AS decimal(10,6))) / (Select COUNT(id_order) FROM ps5_order_detail WHERE id_order = os.id_order)
WHEN od.id_customization!=0 AND s.id_shop=6 THEN SUM(osd.amount_tax_excl) / (Select COUNT(id_order) FROM ps5_order_detail WHERE id_order = os.id_order)
WHEN od.id_customization!=0 AND s.id_shop!=3 THEN SUM(CAST(os.amount / 1.2 AS decimal(10,6))) / (Select COUNT(id_order) FROM ps5_order_detail WHERE id_order = os.id_order)
ELSE SUM(osd.amount_tax_excl) / (Select COUNT(id_order) FROM ps5_order_detail WHERE id_order = os.id_order)
END) AS amount_tax_excl,
SUM(osd.amount_tax_incl)  / (Select COUNT(id_order) FROM ps5_order_detail WHERE id_order = os.id_order) AS amount_tax_incl,
os.shipping_cost_amount, os.partial,
os.date_add, os.date_upd, e.`iso_code` AS `ISO Etat livraison`, e.`name` AS `Name Etat livraison`, a.city, o.`payment` AS `payment`
FROM  ps5_order_slip os
LEFT JOIN ps5_orders o ON (os.id_order = o.id_order)
LEFT JOIN  ps5_order_detail od ON (os.id_order = od.id_order)
[...]

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