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
 SQL Server Development (2000)
 Multiple Joins

Author  Topic 

davidpardoe
Constraint Violating Yak Guru

324 Posts

Posted - 2001-07-18 : 05:28:33
Can anyone manage the following more efficiently than my first stab...

I have the following tables:-

Customer
--------
cust_ref
linked_cust_ref

Limit
-----
cust_ref
limit

The customer table has a unique ref number (cust_ref). If the customer is linked to another (ie. through a joint account) then there will be a linked ref number (linked_cust_ref).

I need to calculate the total limit for the customer by adding the limits for cust_ref and linked_cust_ref.

My implementation at the moment involves creating temporary tables, first for the cust_ref and then for the linked_cust_ref and then merging these into a final table. Anyone come up with a way of doing it in less than 3 selects:-

select customer.cust_ref,limit.limit
into primary_limit
from customer on customer.cust_ref=limit.cust_ref

select customer.cust_ref,limit.limit
into secondary_limit
from customer on customer.linked_cust_ref=limit.cust_ref

select primary_limit.cust_ref,total_limit=primary_limit.limit+seconday_limit.limit
from primary_limit on primary_limit.cust_ref=secondary_limit.cust_ref




   

- Advertisement -