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 2005 Forums
 Transact-SQL (2005)
 Rewrite query

Author  Topic 

lols
Posting Yak Master

174 Posts

Posted - 2009-08-10 : 12:26:31
How can I rewrite this query in the best possible way and using features of SQL Server 2005 or 2008

select b.Name,a.PaymentId,a.salMon,*
from
(
Select tt.PaymentId, tt.Paycode, Avg(sal) as [Range]
from temptbl tt
group by tt.PaymentId
) a
inner Join
temptbl1 b on a.PaymentId = b.PaymentId

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2009-08-10 : 12:32:25
Your query should be giving a error due to not haveing tt.paycode in the group by.

There is nothing wrong with the query as far as methodolgy, just make sure you have proper index's, and depending on the amount of records in temptbl you may consider filtering the data first.


select b.Name,a.PaymentId,a.salMon,*
from
(
Select tt.PaymentId, tt.Paycode, Avg(sal) as [Range]
from temptbl tt
group by tt.PaymentId,tt.paycode
) a
inner Join
temptbl1 b on a.PaymentId = b.PaymentId



Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

lols
Posting Yak Master

174 Posts

Posted - 2009-08-10 : 12:45:47
Can same be written using CTE or any other feature?
Go to Top of Page
   

- Advertisement -