Here's the scenario: there is a software table (PK = SoftwareID) and its associated Release table (PK = [SoftwareID, Version]).
The release can be major or minor, the type of release is identified by Release.ReleaseType ('MAJ', 'MIN').
The release is also characterized by the date: Release.ReleaseDate.
The software is divided into categories identified by Software.CategoryID.
Task: to require an effective T-SQL query to list all pieces of software in a certain category and have the first significant release date falling within this interval, divided by @DateFrom, @DateTo. The only columns needed in the final result set are SoftwareID and ReleaseDate.
This is not a real scenario, but I have formulated it in such a way that it is easier to understand. In the real case, the Release table will have about 10 million records, and the Software table will have about 1 million. I came up with a solution, but it is rather slow, and I feel that the experts here can find something better.
Here is 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.
source
share