-
leetcode - 1440. Evaluate Boolean ExpressionSQL 문제풀이 2022. 8. 12. 17:52
난이도 : Medium
Table Variables:
+---------------+---------+ | Column Name | Type | +---------------+---------+ | name | varchar | | value | int | +---------------+---------+ name is the primary key for this table. This table contains the stored variables and their values.
Table Expressions:
+---------------+---------+ | Column Name | Type | +---------------+---------+ | left_operand | varchar | | operator | enum | | right_operand | varchar | +---------------+---------+ (left_operand, operator, right_operand) is the primary key for this table. This table contains a boolean expression that should be evaluated. operator is an enum that takes one of the values ('<', '>', '=') The values of left_operand and right_operand are guaranteed to be in the Variables table.
Write an SQL query to evaluate the boolean expressions in Expressions table.
Return the result table in any order.
The query result format is in the following example.
Example 1:
Input: Variables table: +------+-------+ | name | value | +------+-------+ | x | 66 | | y | 77 | +------+-------+ Expressions table: +--------------+----------+---------------+ | left_operand | operator | right_operand | +--------------+----------+---------------+ | x | > | y | | x | < | y | | x | = | y | | y | > | x | | y | < | x | | x | = | x | +--------------+----------+---------------+ Output: +--------------+----------+---------------+-------+ | left_operand | operator | right_operand | value | +--------------+----------+---------------+-------+ | x | > | y | false | | x | < | y | true | | x | = | y | false | | y | > | x | true | | y | < | x | false | | x | = | x | true | +--------------+----------+---------------+-------+ Explanation: As shown, you need to find the value of each boolean expression in the table using the variables table.
SELECT e.left_operand , e.operator , e.right_operand , CASE WHEN operator = '>' THEN IF(v1.value > v2.value, 'true', 'false') WHEN operator = '<' THEN IF(v1.value < v2.value, 'true', 'false') ELSE IF(v1.value = v2.value, 'true', 'false') END value FROM Expressions e JOIN Variables v1 ON e.left_operand = v1.name JOIN Variables v2 ON e.right_operand = v2.name
Accepted (8.08%)
'SQL 문제풀이' 카테고리의 다른 글
leetcode - 2339. All the Matches of the League (0) 2022.08.15 leetcode - 1445. Apples & Oranges (0) 2022.08.12 leetcode - 2329. Product Sales Analysis V (0) 2022.08.11 leetcode - 2230. The Users That Are Eligible for Discount (0) 2022.08.11 leetcode - 2205. The Number of Users That Are Eligible for Discount (0) 2022.08.11