https://www.youtube.com/watch?v=u9p6LyE4nGk

-- Drop the table if it exists
DROP TABLE IF EXISTS Combo;

-- Create the Combo table
CREATE TABLE Combo (
    Combo NVARCHAR(MAX),
    Cost INT
);

-- Insert data into the Combo table
INSERT INTO Combo (Combo, Cost)
VALUES 
    ('A,B,C', 90),
    ('B,C,A', 20),
    ('C,A,B', 30),
    ('Z,X,Y', 10),
    ('X,Z,Y', 20),
    ('X,Y,Z', 30),
    ('P,X,Y', 100),
    ('X,P,Y', 200);

Pasted image 20241127113831.png

WITH
SUBSTRING_INDEX()

with cte as (
select * 
	,substring_index(combo,',',1) as first
    ,substring_index(substring_index(combo,',',2),',',-1) as second
    ,substring_index(combo,',',-1) as third
from combo
)
select combo,cost 
from cte
where (first<second and second<third)