Help with query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harvindersingh
    New Member
    • Sep 2008
    • 24

    Help with query

    Hello

    I am trying to compile a query but getting inconsistent data from it. I need to make use of two tables namely:

    transaction_pro ducts [contains products related to a transaction in transactions table]
    refunds [contains refunded product details related to a transaction in transactions table]

    Both of the above tables contain product_id and I would like to join the tables to gather some detailed information about sales. I know it sounds like a simple job but I keep getting inconsistent results from the join, here is the sample query I am using to join:

    SELECT transaction_pro ducts.id, sum(transaction _products.qty), sum(refunds.pri ce) FROM refunds NATURAL JOIN transaction_pro ducts GROUP BY transaction_pro ducts.id;

    Now when I run a group by query on a single table I get the sum calculated fine, however after doing a join like the above I get the amounts to be higher than their original sum in all the products for both the qty and the price. I am not sure where I am going wrong with this, could someone pleas e shed some light on this problem and put me in the right direction.

    Thanks
  • rski
    Recognized Expert Contributor
    • Dec 2006
    • 700

    #2
    The query looks ok. It would be hard to diagnose the problem without the table data. But I guess tables are too big :) to put it here.

    Comment

    • harvindersingh
      New Member
      • Sep 2008
      • 24

      #3
      Hello

      Thanks for replying, apparently it was due to bad data in the refunds table which was messing up the results for the JOIN. After trying "NATURAL FULL JOIN" things seemed to start working and the results are looking fine aswell. I think it was rather a human error :) in trying to understand the formation of the JOIN.

      Thanks

      Comment

      Working...