|
| 1 | +-- Question 78 |
| 2 | +-- Table Variables: |
| 3 | + |
| 4 | +-- +---------------+---------+ |
| 5 | +-- | Column Name | Type | |
| 6 | +-- +---------------+---------+ |
| 7 | +-- | name | varchar | |
| 8 | +-- | value | int | |
| 9 | +-- +---------------+---------+ |
| 10 | +-- name is the primary key for this table. |
| 11 | +-- This table contains the stored variables and their values. |
| 12 | + |
| 13 | + |
| 14 | +-- Table Expressions: |
| 15 | + |
| 16 | +-- +---------------+---------+ |
| 17 | +-- | Column Name | Type | |
| 18 | +-- +---------------+---------+ |
| 19 | +-- | left_operand | varchar | |
| 20 | +-- | operator | enum | |
| 21 | +-- | right_operand | varchar | |
| 22 | +-- +---------------+---------+ |
| 23 | +-- (left_operand, operator, right_operand) is the primary key for this table. |
| 24 | +-- This table contains a boolean expression that should be evaluated. |
| 25 | +-- operator is an enum that takes one of the values ('<', '>', '=') |
| 26 | +-- The values of left_operand and right_operand are guaranteed to be in the Variables table. |
| 27 | + |
| 28 | + |
| 29 | +-- Write an SQL query to evaluate the boolean expressions in Expressions table. |
| 30 | + |
| 31 | +-- Return the result table in any order. |
| 32 | + |
| 33 | +-- The query result format is in the following example. |
| 34 | + |
| 35 | +-- Variables table: |
| 36 | +-- +------+-------+ |
| 37 | +-- | name | value | |
| 38 | +-- +------+-------+ |
| 39 | +-- | x | 66 | |
| 40 | +-- | y | 77 | |
| 41 | +-- +------+-------+ |
| 42 | + |
| 43 | +-- Expressions table: |
| 44 | +-- +--------------+----------+---------------+ |
| 45 | +-- | left_operand | operator | right_operand | |
| 46 | +-- +--------------+----------+---------------+ |
| 47 | +-- | x | > | y | |
| 48 | +-- | x | < | y | |
| 49 | +-- | x | = | y | |
| 50 | +-- | y | > | x | |
| 51 | +-- | y | < | x | |
| 52 | +-- | x | = | x | |
| 53 | +-- +--------------+----------+---------------+ |
| 54 | + |
| 55 | +-- Result table: |
| 56 | +-- +--------------+----------+---------------+-------+ |
| 57 | +-- | left_operand | operator | right_operand | value | |
| 58 | +-- +--------------+----------+---------------+-------+ |
| 59 | +-- | x | > | y | false | |
| 60 | +-- | x | < | y | true | |
| 61 | +-- | x | = | y | false | |
| 62 | +-- | y | > | x | true | |
| 63 | +-- | y | < | x | false | |
| 64 | +-- | x | = | x | true | |
| 65 | +-- +--------------+----------+---------------+-------+ |
| 66 | +-- As shown, you need find the value of each boolean exprssion in the table using the variables table. |
| 67 | + |
| 68 | +-- Solution |
| 69 | +with t1 as( |
| 70 | +select e.left_operand, e.operator, e.right_operand, v.value as left_val, v_1.value as right_val |
| 71 | +from expressions e |
| 72 | +join variables v |
| 73 | +on v.name = e.left_operand |
| 74 | +join variables v_1 |
| 75 | +on v_1.name = e.right_operand) |
| 76 | + |
| 77 | +select t1.left_operand, t1.operator, t1.right_operand, |
| 78 | +case when t1.operator = '<' then (select t1.left_val< t1.right_val) |
| 79 | +when t1.operator = '>' then (select t1.left_val > t1.right_val) |
| 80 | +when t1.operator = '=' then (select t1.left_val = t1.right_val) |
| 81 | +else FALSE |
| 82 | +END AS VALUE |
| 83 | +from t1 |
0 commit comments