I have two database tables:
Table "public.table_1"
Column | Type | Modifiers
------------+---------+-----------
id | integer |
value | integer |
date_one | date |
date_two | date |
date_three | date |
Table "public.table_2"
Column | Type | Modifiers
------------+---------+-----------
id | integer |
table_1_id | integer |
selector | text |
The values ββin table_2.selectorcan be one of one, twoor threeand are used to select one of the date columns in table_1.
In my first implementation I used CASE:
SELECT value
FROM table_1
INNER JOIN table_2 ON table_2.table_1_id = table_1.id
WHERE CASE table_2.selector
WHEN 'one' THEN
table_1.date_one
WHEN 'two' THEN
table_1.date_two
WHEN 'three' THEN
table_1.date_three
ELSE
table_1.date_one
END BETWEEN ? AND ?
The values ββfor selectorare such that I could identify the column of interest as eval(date_#{table_2.selector})if PL / pgSQL allows me to evaluate rows as expressions.
The closest I could find is EXECUTE stringone that evaluates whole statements. Is there any way to evaluate expressions?
source
share