I'm trying to create a query that will return a list of results based on a series of conditions. For example – a list of movies based on interests/preferences.
Example Source Data:
+--------+---------------+-------+-------------+--------+
|MovieID | ShortName | Genre | Time(Mins) | Action |
+--------+---------------+-------+-------------+--------+
| 1 | LOTR 1 |Fantasy| 208 | TRUE |
+--------+---------------+-------+-------------+--------+
| 2 | LOTR 2 |Fantasy| 223 | TRUE |
+--------+---------------+-------+-------------+--------+
| 18 | Harry Potter 1|Fantasy| 174 | TRUE |
+--------+---------------+-------+-------------+--------+
| 45 | Love Actually |Romance| 145 | FALSE |
+--------+---------------+-------+-------------+--------+
A separate table contains a limited series of filters directly related to columns of data. For example:
Filter 1: ShortName (Contains) "LOTR" && Time (IsGreaterThan) 220 Minutes
Filter 2: ShortName (Equals) "LOTR 2"
Filter 3: Action (Is) "FALSE" && Time (IsGreaterThan) 60 Minutes
Filter 4: Genre (IsNot) "Fantasy" && Time (IsLessThan) 180 Minutes
Filter 1 returns: MovieID (1,2)
Filter 2 returns: MovieID (2)
Filter 3 returns: MovieID (45)
Filter 4 returns: MovieID (45)
Ignore the way results are shown above, it's just to exemplify the returned values.
I need the table of results to be returned in 1 execution of the query.
I'm skilled enough in SQL, however due to legacy platform constraints, I have to achieve this without using looping (While/For) functions.
Is there a name for this kind of query (to return a list of values from a table based on a complex filter statement), and can someone provide an example query to help me to understand the concept.
Please let me know in the comments if you require more information to help.
Best Answer
fiddle
Pay attention to formalized filters storing format.
You may add possible
operator
/criteria
values by adding according variants into CASEs.For example, if you want to add a filtering variant for to select strict time you must add
WHEN 'Equal' THEN Time_Mins = criteria
toTime_Mins
checkingCASE
.But do not forget that
criteria
datatype is STRING! AndAction
datatype is string too. Use proper data type convertion when default convertion is not obvious.