November 28, 2017 at 7:10 am
SELECT id_product, SUM(COALESCE(received_qty, ordered_qty)) AS total
FROM (Select id_department,id_product,received_qty, ordered_qty from
(SELECT * FROM cart_item WHERE id_cart IN (SELECT id_cart FROM purchase)) AS c
LEFT JOIN Purchase p on p.id_cart = c.id_cart ) as Z
GROUP BY id_product
http://sqlfiddle.com/#!9/58cdd3/1
I am trying to get the total sum of qty from ordered_qty and received_qty, but it only sum the id_cart = 1 and ignored other id_cart.
What's wrong with my query? Thanks.
expected output : show total of quantity based on id_product from different purchase.
November 28, 2017 at 7:18 am
Duplicate post. direct replies here. https://www.sqlservercentral.com/Forums/1909881/Need-help-on-query-when-sum-of-total-show-inaccurate-result
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply