Home Forums Programming General Need help on query when sum of total show inaccurate result RE: Need help on query when sum of total show inaccurate result

  • testingpoke1 - Tuesday, November 28, 2017 8:05 AM

    I am doing analysis for the quantity of product purchased. I have few table to join to get other details from the table. As you can see from the sql fiddle, i am trying to sum the quantity of product received, and if quantity received is null then will get the value of ordered quantity. The output show is incorrect since it only shows me the id cart = 1. By right i want it to show both cart id 1 and 2 and sum of quantity of product id..

    Well look at your query (which is way more complicated than it needs to be). You have:

    SELECT * FROM cart_item WHERE id_cart IN (SELECT id_cart FROM purchase)

    This by definition will prevent id_product 3 from appearing in your results because you do not have a row in the purchase table for id_cart 2. I am willing to help you but you have to explain what it is you are trying to do here.

    _______________________________________________________________

    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/