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)
 self join to calculate value

Author  Topic 

thanksfor help
Posting Yak Master

106 Posts

Posted - 2008-02-22 : 18:39:22
Hi,

I have table which have to be self joined to do some calculation. here is sample of the table

aid aname monthid region amount
1 sales jan asia 1000
2 sales1 jan asia 100
1 sales jan europe 2000
2 sales1 jan europe 200
1 sales jan us 3000
2 sales1 jan us 300

percentage = (sales/sales1) * 100

select a.amount, b.amount from tbl1 a
inner join tbl1 b on a.monthid = b.monthid and a.region = b.region and a.acid <> b.acid
where a.acid in (1,2) and b.acid in (1,2)
---- because there are other account type in the table
and a.acid < b.acid

This query works perfect, but I want to know is there any other best alternative as the table size is growing fast.

thanks in advance.

bbasir
Yak Posting Veteran

76 Posts

Posted - 2008-02-22 : 18:47:52
have you created indexes on the where condition fields? and join fields.. that woudl help.
Go to Top of Page
   

- Advertisement -