Are you saying that within each of those data sets there might be a key that is the same (KeyID) and that you only want one row with said Key? Furthermore, precedence should be given to the row/dataset where the KeyType is equal to "so"?If that is the case, then here is one way:SELECT *FROM ( SELECT * ,ROW_NUMBER() OVER (PARTITION BY KeyID ORDER BY CASE WHEN KeyType = 'so' THEN 0 ELSE 1 END) AS RowNum FROM ( SELECT keytype = 'so' --sales order ,keyid = salesid ,site = ViewName.inventsiteid ,ViewName.dataareaid AS dataareaid FROM ViewName UNION ALL SELECT keytype = 'to' --transfer order ,keyid = TableName1.transferid ,site = TableName2.INVENTLOCATIONIDTRANSIT ,TableName1.dataareaid AS dataareaid FROM TableName1 LEFT JOIN TableName2 ON TableName2.TRANSFERID = TableName1.TRANSFERID ) AS A ) AS B