Using SQL Server 2005, I am attempting to write a select statement which will query the data below (18000 rows) and return one row per Product, finding the largest product version.
I think the query needs to compare the versions in reverse, and storing the largest row number as it gets up to Major but I am not sure.
Sample Data
Product Date Major Minor Release Fixpack Build
======= ========== ====== ====== ====== ====== ======
Widgets 01/01/2012 10 1 0 345 132
Widgets 04/02/2011 6 6 3 932 245
Widgets 09/01/2010 4 92 345 53 2
Gears 03/17/2011 9 2 3 0 21
Gears 12/12/2012 10 0 0 5 548
Tires 04/24/2000 8 3 456 3 984
Tires 04/12/1993 7 14 45 35 252
Expected Results
Product Date Major Minor Release Fixpack Build
======= ========== ====== ====== ====== ====== ======
Widgets 01/01/2012 10 1 0 345 132
Gears 12/12/2012 10 0 0 5 548
Tires 04/24/2000 8 3 456 3 984
OR even better, could combine the versions into one column:
Product Date ProductVersion
======= ========== ==================
Widgets 01/01/2012 10.1.0.345.132
Gears 12/12/2012 10.0.0.5.548
Tires 04/24/2000 8.3.456.3.984
You can use the ROW_NUMBER function in a derived table. Do the partition on
Productand order byDate descor your version information.Ordered by
Date desc:Ordered by
ProductVersion descSQL Fiddle