Search code examples
sql-serversql-like

Comparing the length of two similar strings and picking the longest


I am Trying to compare two strings and pick the longest if they are similar, I have managed to pick the longest by using the following code:

SELECT D.RID, ProductID, Product, [Length] FROM (
SELECT RID, MAX([Length]) AS theLength FROM SortData GROUP BY RID)
AS X INNER JOIN SortData AS D ON D.RID = X.RID AND D.[Length] = X.theLength

But I am now trying to make sure that the code only pick the longest string if it is a like the word it is comparing it to, I have attempted the following code in a few ways but I would be grateful if somebody could help me:

SELECT D.RID, D.ProductID, Product, [Length] FROM (
SELECT RID, Product, MAX([Length]) AS theLength FROM SortData GROUP BY RID)
AS X INNER JOIN SortData AS D ON D.RID = X.RID AND D.[Length] = X.theLength WHERE
D.Product LIKE Product

Using this code I get the Following Error:

Msg 8120, Level 16, State 1, Line 3 Column 'SortData.Product' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. Msg 209, Level 16, State 1, Line 5 Ambiguous column name 'Product'. Msg 209, Level 16, State 1, Line 2 Ambiguous column name 'Product'.

Example of the Data I would Like to pick:

1 Sam
1 Samantha
2 Oliver
3 Ollie
4 Benjamin
4 Ben
...
I would expect the output list to be like:

1 Samantha
2 Oliver
3 Ollie
4 Benjamin
...

To Clarify what I am trying to do in the context of this example, I am trying to compare the two Names and if the are LIKE (e.g. x.Name LIKE Name) then pick the longest...

As Requested here is further test data:

1 Hydrogen
1 Hydrogen Oxide
1 Carbon Monoxide
2 Carbon
2 Carbon
2 Carbon Dioxide
3 Carbon Monoxide
3 Carbon Dioxide
3 Oxygen
4 Hydrogen Dioxide

Desired Results are as so:

1 Hydrogen Oxide
1 Carbon Monoxide
2 Carbon Dioxide
3 Carbon Monoxide
3 Oxygen
4 Hydrogen Dioxide


Solution

  • I Ended up figuring it out and using the following code:

    SELECT D.RID, ProductID, D.Product, [Length] FROM 
    (
    SELECT RID, MAX([Length]) AS theLength 
    FROM SortData GROUP BY RID
    ) AS X 
    INNER JOIN SortData AS D ON D.RID = X.RID AND D.[Length] = X.theLength
    WHERE D.Product LIKE Product
    GO