Here's the scenario: there is a Software table (PK = SoftwareID) and an associated Release table (PK = [SoftwareID,Version]).
A release can be major or minor, release type is identified by Release.ReleaseType ('MAJ', 'MIN').
A release is also characterized by a date: Release.ReleaseDate.
Software is partitioned into categories, identified by Software.CategoryID.
Problem: need an effective T-SQL query to list all software pieces of a certain category and having the first major release date falling inside a given interval, delimited by @DateFrom, @DateTo. The only columns needed in the final resultset are SoftwareID and ReleaseDate.
This is not the real case scenario but I formulated it this way to be easier to understand. In the real case the table Release would have around 10 million records and the table Software around 1 million. I came up already with a solution but it's quite slow and I feel the experts around here might find something better.
Here's my slow solution:
select s.SoftwareID, min(r.ReleaseDate)
from
Software s inner join Release r on (s.SoftwareID = r.SoftwareID)
where s.CategoryID = @Category
and r.ReleaseType = 'MAJ'
group by
s.SoftwareID
having
min(r.ReleaseDate) >= @DateFrom
and min(r.ReleaseDate) < @DateTo
Thanks.