I have a table with columns
Index, Date
where an Index may have multiple Dates, and my goal is the following: select a list that looks like
Index, MinDate, MaxDate
where each Index is listed only once, and MinDate (MaxDate) represents the earliest (latest) date present in the entire table for that index. That’s easy enough, but then let’s constrain this list to appear only for Indexes that are present in a given range of dates.
So far, I have the following:
SELECT Index, MIN([Date]), MAX([Date]) FROM myTable WHERE Index IN (SELECT Index From myTable WHERE [Date] BETWEEN '1/1/2000' AND '12/31/2000') GROUP BY Index ORDER BY Index ASC
This is excruciatingly slow. Any way to speed this up? [I am running SQL Server 2000.]
Thanks!
Edited: For clarity.
I would recommend a derived table approach. Like this:
EDIT: Upon further review, there is another way you can create this query. The following query may be faster, slower, or execute in the same amount of time. This, of course, depends on how the table is indexed.
Under the best circumstances, this query will cause an index scan (not a seek) to filter out rows you don’t want to display. I encourage you to run both queries and pick this oen the executes the fastest.