Connecting Tech Pros Worldwide Help | Site Map

Help with query

Newbie
 
Join Date: Sep 2008
Location: London, England
Posts: 24
#1: May 9 '09
Hello

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

transaction_products [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_products.id, sum(transaction_products.qty), sum(refunds.price) FROM refunds NATURAL JOIN transaction_products GROUP BY transaction_products.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
Moderator
 
Join Date: Dec 2006
Location: Europe
Posts: 292
#2: May 10 '09

re: Help with query


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.
Newbie
 
Join Date: Sep 2008
Location: London, England
Posts: 24
#3: May 10 '09

re: Help with query


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
Reply


Similar PostgreSQL Database bytes