Question : Need SQL Syntax help to make my qry more efficient.

Friends,

Attached, I have my qry.  As you can see, I have to do a selection to evaluate the value returned.

Is there a more effcient way that I can determine OverallBestLapTime and more importantly FormattedOverallBestLapTime.

Looking for SQL Syntax help specifically!

Thanks in advance!

Eric
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
Select
  [No]=rtrim(No),
  NameF=rtrim(FirstName),
  NameL=rtrim(LastName),
  BestLapTime,
  OverallBestLapTime=(select min(r2.bestlaptime) from Results r2 where R2.No=r1.No and r2.LastName=r1.LastName and r2.Firstname=R1.FirstName and Class='IndyCar' and bestlaptime >-1 ),
	FormattedOverallBestLapTime=(Case
			When ((select min(r2.bestlaptime) from Results r2 where R2.No=r1.No and r2.LastName=r1.LastName and r2.Firstname=R1.FirstName and Class='IndyCar' and bestlaptime >-1 )*0.0001)<61 Then
					str(((select min(r2.bestlaptime) from Results r2 where R2.No=r1.No and r2.LastName=r1.LastName and r2.Firstname=R1.FirstName and Class='IndyCar' and bestlaptime >-1 ) * 0.0001),8,4)				
			Else
				dbo.FormatLapTime((select min(r2.bestlaptime) from Results r2 where R2.No=r1.No and r2.LastName=r1.LastName and r2.Firstname=R1.FirstName and Class='IndyCar' and bestlaptime >-1 ))
			END),
ResultID as ResultID
from
  Results R1
where
  lastlaptime>-2 and deleted=0
	And RunID=8

Answer : Need SQL Syntax help to make my qry more efficient.

Select
  [No]=rtrim(No),
  NameF=rtrim(FirstName),
  NameL=rtrim(LastName),
  BestLapTime,
  OverallBestLapTime=(r2.bestlaptime) ,
  FormattedOverallBestLapTime=(Case
              When (r2.bestlaptime *0.0001)<61 Then
                          str( r2.bestlaptime * 0.0001,8,4)                        
              Else
                    dbo.FormatLapTime(r2.bestlaptime)
              END),
ResultID as ResultID
from  Results R1
RIGHT JOIN
(select No,LastName,Firstname, min(r2.bestlaptime) bestlaptime  
 from Results r2 where  Class='IndyCar' and bestlaptime >-1
 GROUP BY No,LastName,Firstname ) as   R2 on R2.No=r1.No and r2.LastName=r1.LastName and r2.Firstname=R1.FirstName
where   lastlaptime>-2 and deleted=0  And RunID=8
Random Solutions  
 
programming4us programming4us