|
lazerath
Constraint Violating Yak Guru
343 Posts |
Posted - 2010-11-16 : 17:07:11
|
| Here's my crazy solution. It relies on you having a limited set of operators that can ideally be enforced with a constraint (=,<,<=,>,>=). If there are other operators you want to support, you could simply modify the logic, but each operator you add becomes a code change. [CODE]IF OBJECT_ID('tempdb.dbo.#filters') IS NOT NULL DROP TABLE #filters;IF OBJECT_ID('tempdb.dbo.#answers') IS NOT NULL DROP TABLE #answers;CREATE TABLE #filters(filterID int,questionID int,operator varchar,constant float)CREATE TABLE #answers(answerID int,questionID int,itemID int,answer float)INSERT #filtersVALUES (1,13,'<',50),(2,14,'=',45);INSERT #answersVALUES(1, 13, 1,10),(2, 14, 1,45),(3, 13, 2,50),(4, 14, 2,45),(5, 13, 3,20),(6, 14, 3,46)DECLARE @min_increment float = 1E-14;DECLARE @low_bound float = -1.79E+308;DECLARE @high_bound float = 1.79E+308;-- Option 1 -- VERY LIMITED EXAMPLE showing BETWEEN-- Greatly depends on how many operators you will allowSELECT a.itemIDFROM #filtersAS fJOIN #answersAS aON f.questionID= a.questionIDGROUP BY a.itemIDHAVING SUM ( CASE WHEN a.answer BETWEEN CASE f.operator WHEN '<' THEN @low_bound WHEN '<=' THEN @low_bound WHEN '>' THEN f.constant + @min_increment WHEN '>=' THEN f.constant ELSE f.constant END AND CASE f.operator WHEN '<' THEN f.constant - @min_increment WHEN '<=' THEN f.constant WHEN '>' THEN @high_bound WHEN '>=' THEN @high_bound ELSE f.constant END THEN 0 ELSE 1 END ) = 0IF OBJECT_ID('tempdb.dbo.#filters') IS NOT NULL DROP TABLE #filters;IF OBJECT_ID('tempdb.dbo.#answers') IS NOT NULL DROP TABLE #answers;[/CODE] |
 |
|