Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Experimenting with RIGHT JOIN

Author  Topic 

myles.musser
Starting Member

1 Post

Posted - 2007-08-13 : 15:43:03
Hi all,

I'm pretty new to anything beyond your basic select, update and insert statements and am trying to move onto more advanced queries (well, more advanced for me, anyways). I'm running a query trying to figure out refund rates for certain products. The number of purchases are in tPurchase, and the refunds are in tRefund, and I want to get the number of each products' purchases versus their refunds for a given month. Here's what I'm running:

use billing
select tPurchase.productid,count(*) as transcnt, tRefund.productid,count(*) as refndcnt
From tPurchase
RIGHT JOIN tRefund
ON tPurchase.productid=tRefund.productid
where tPurchase.tssubmitted between '2007-07-01' and '2007-08-01'
and tRefund.tscreated between '2007-07-01' and '2007-08-01'
group by tPurchase.productid,tRefund.shortcode

I can run this without errors, but I don't really get the results I'm looking for. The count data I get from the two tables is identical (which would indicate that we've got 100% refunds on certain products, which I know is not the case). Any ideas on what I'm doing wrong here? This is my first venture into using RIGHT JOIN.

Thanks in advance for any help,

~Myles

ashley.sql
Constraint Violating Yak Guru

299 Posts

Posted - 2007-08-13 : 16:03:20
can you post some sample data and required result along with script to create table and few inserts in the database.

see the rules of posting or help on posting before you post

Ashley Rhodes
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2007-08-13 : 16:29:02
I just did a quick glance, but it appears that if you change RIGHT JOIN to LEFT JOIN you would get the results you want. The difference between the two is the order of the tables surrounding the JOIN. In this case the order of tPurchase and tRefund. If you want all the purchases with their associated refunds you would do: tPurchases LEFT JOIN tRefund or tRefund RIGHT JOIN tPurchases.

You might want to try playing with a smaller data set so you can see the affect of using each join.

Not sure if this helps, but from BOL: The Right Outer Join operator returns each row that satisfies the join of the second (bottom) input with each matching row from the first (top) input. It also returns any rows from the second input that had no matching rows in the first input, joined with NULL. If no join predicate exists in the Argument column, each row is a matching row.

Where as The Left Outer Join operator returns each row that satisfies the join of the first (top) input with the second (bottom) input. It also returns any rows from the first input that had no matching rows in the second input. The nonmatching rows in the second input are returned as null values. If no join predicate exists in the Argument column, each row is a matching row.

-Ryan
Go to Top of Page

pootle_flump

1064 Posts

Posted - 2007-08-14 : 04:54:59
http://weblogs.sqlteam.com/jeffs/archive/2006/03/14/9289.aspx
Go to Top of Page
   

- Advertisement -