This is SQL Server code.
Let's say you have a table with three columns. Column 1 is named Monster, Column 2 is named Level, and Column 3 is named BodyType. Level signifies how powerful the monster is and BodyType signifies what type of body it has.
My schema:
CREATE TABLE YourTable
([Monster] nvarchar(max), [Level] int, [BodyType] nvarchar(max))
;
INSERT INTO YourTable
([Monster], [Level], [BodyType])
VALUES
('Small Beast', 300, 'Scaly'),
('Large Beast', 700, 'Slimy'),
('Small Dragon', 350, 'Fiery'),
('Large Dragon', 800, 'Slimy')
;
I have a sql command to find all possible combinations of the monsters. It uses recursive cte because the number of monsters in the table can fluctuate(so I can add more monsters later on). The command also grabs the sum total value of the Level of the monsters that are being combined. The command also only outputs the combinations of monsters that fall under a certain total sum value. In this example, that total sum value is 1500. Everything up to this point works as it should.
My sql command:
;WITH cte AS (
SELECT Monster,
[Level],
BodyType,
1 as l
FROM YourTable
UNION ALL
SELECT c1.Monster+','+c2.Monster,
c1.[Level]+c2.[Level],
c1.BodyType+','+c2.BodyType,
c1.l+1
FROM cte c1
CROSS JOIN YourTable c2
WHERE c1.Monster NOT LIKE '%'+c2.Monster+'%'
)
SELECT *
FROM cte
WHERE cte.Level < 1500
ORDER BY l
OPTION (MAXRECURSION 0)
And the correct output:
1 Small Beast 300 Scaly 1
2 Large Beast 700 Slimy 1
3 Small Dragon 350 Fiery 1
4 Large Dragon 800 Slimy 1
5 Large Dragon,Small Beast 1100 Slimy,Scaly 2
6 Large Dragon,Small Dragon 1150 Slimy,Fiery 2
7 Small Dragon,Small Beast 650 Fiery,Scaly 2
8 Small Dragon,Large Beast 1050 Fiery,Slimy 2
9 Small Dragon,Large Dragon 1150 Fiery,Slimy 2
10 Large Beast,Small Beast 1000 Slimy,Scaly 2
11 Large Beast,Small Dragon 1050 Slimy,Fiery 2
12 Small Beast,Large Beast 1000 Scaly,Slimy 2
13 Small Beast,Small Dragon 650 Scaly,Fiery 2
14 Small Beast,Large Dragon 1100 Scaly,Slimy 2
15 Small Beast,Large Dragon,Small Dragon 1450 Scaly,Slimy,Fiery 3
16 Small Beast,Small Dragon,Large Beast 1350 Scaly,Fiery,Slimy 3
17 Small Beast,Small Dragon,Large Dragon 1450 Scaly,Fiery,Slimy 3
18 Small Beast,Large Beast,Small Dragon 1350 Scaly,Slimy,Fiery 3
19 Large Beast,Small Dragon,Small Beast 1350 Slimy,Fiery,Scaly 3
20 Large Beast,Small Beast,Small Dragon 1350 Slimy,Scaly,Fiery 3
21 Small Dragon,Large Dragon,Small Beast 1450 Fiery,Slimy,Scaly 3
22 Small Dragon,Large Beast,Small Beast 1350 Fiery,Slimy,Scaly 3
23 Small Dragon,Small Beast,Large Beast 1350 Fiery,Scaly,Slimy 3
24 Small Dragon,Small Beast,Large Dragon 1450 Fiery,Scaly,Slimy 3
25 Large Dragon,Small Dragon,Small Beast 1450 Slimy,Fiery,Scaly 3
26 Large Dragon,Small Beast,Small Dragon 1450 Slimy,Scaly,Fiery 3
The problem I am encountering is when I add a Where clause to only bring back monsters that are not of a certain body type(BodyType column). That part of the code from above when modified to accomplish this is:
;WITH cte AS (
SELECT Monster,
[Level],
BodyType,
1 as l
FROM YourTable
WHERE BodyType NOT LIKE 'Fiery' AND BodyType NOT LIKE 'Slimy'
UNION ALL
The output becomes the following which is incorrect because it still includes the body types of Slimy and Fiery:
Monster Level BodyType l
1 Small Beast 300 Scaly 1
2 Small Beast,Large Beast 1000 Scaly,Slimy 2
3 Small Beast,Small Dragon 650 Scaly,Fiery 2
4 Small Beast,Large Dragon 1100 Scaly,Slimy 2
5 Small Beast,Large Dragon,Small Dragon 1450 Scaly,Slimy,Fiery 3
6 Small Beast,Small Dragon,Large Beast 1350 Scaly,Fiery,Slimy 3
7 Small Beast,Small Dragon,Large Dragon 1450 Scaly,Fiery,Slimy 3
8 Small Beast,Large Beast,Small Dragon 1350 Scaly,Slimy,Fiery 3
The output seems to be working partially since Large Beast is Slimy and it ignored it the first time but I suspect it is ignoring the NOT LIKE clause when moving through the levels of BodyType and that's why it doesn't ignore Large Beast on subsequent finds.