I have a statement that SELECTs distinct rows from a table with a two value index
SELECT distinct Reference, Value1, Value2, Value3, Value4 FROM [tblHistory]
Where Reference is an index with another field "Project". For a particular system this data is inserted into another table using only Reference as the index because Value1 through Value4 SHOULD always be the same for the same Reference - however in about 1/500 it is not.
In the case where there is a duplicate Reference AND differences in one or more of the Value1-Value4 fields I need to pick the row with the most completed Value1-Value4 fields as they are often NULL. If all instances have the same number of populated columnsI can return the first row found.
Other than using temporary tables and code like
case when Value1 is null then 1 else 0 end
+ case when Value2 is null then 1 else 0 end
+ case when Value3 is null then 1 else 0 end
+ case when Value4 is null then 1 else 0 end
as CountOfNulls
Is there a way to filter the data so I only get the most populated row?
I'm running MS SQL Server 2000.