Sunday, February 19, 2012

Database query performance

I have a database table. The table has number of fields. Out of those fields one is Company and
another is DateTime. The table has thousands of records. I want to get the most recent
record for each company. In order to do that I am using the following query

SELECT * from CompanyDetail AS X
WHERE [DateTime]=(SELECT max([DateTime]) FROM CompanyDetail WHERE Company=X.Company)
ORDER BY Company


Note: There is only one record exists for the given company on a given date

The problem is that this query is very slow. Am I doing something wrong or there could be another
alternative way to improve it ?

Thanks in advance
KDV


Your best bet is to ask this question in the Transact-SQL forum:
http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=85

David Sceppa
Microsoft

No comments:

Post a Comment