-3

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.

I am working on a SQL problem as shown in the above. I used MS SQL server and tried

SELECT
left_operand, operator, right_operand,
IIF(  
(left_values > right_values AND operator = '>') OR 
(left_values < right_values AND operator = '<' ) OR 
(left_values = right_values AND operator = '='), 'true', 'false') as 'value' 
FROM 
(SELECT *,
IIF(left_operand = 'x', (SELECT value FROM Variables WHERE name='x')
                      , (SELECT value FROM Variables WHERE name='y')) as left_values, 
IIF(right_operand = 'x', (SELECT value FROM Variables WHERE name='x')
                      , (SELECT value FROM Variables WHERE name='y')) as right_values
FROM Expressions) temp;

It works well on the test set but gets wrong when I submit it. I think my logic is correct, could anyone help take a look at it and let me know what my problem is?

Thank you!

alli666
  • 11
  • 3
  • We can't see your link unless we are subscribed to premium on that site, therefore not many people are going to be able to help you. – Charleh Mar 05 '22 at 19:04
  • 3
    Even if the site was completely free and accessible, a question on SO must be self-contained. You can extract as little of the problem description as required to illustrate what you're struggling with, but it has to be more than nothing. – Jeroen Mostert Mar 05 '22 at 19:14
  • @Charleh, my bad, I did not notice this is a premium problem. Let me post it. – alli666 Mar 05 '22 at 21:47
  • @JeroenMostert Thank you for letting me know. I just post the whole question. – alli666 Mar 05 '22 at 21:53

1 Answers1

0

It feels like your example code is a lot more complicated than it needs to be. That's probably why it's failing the check. In your FROM you're using sub-selects but really a simple inner join would work much simpler. Also, if there were variables other than X and Y it doesn't look like your example code would work. Here's my code that I wrote in Postgres (should work in any SQL though).

SELECT e.left_operand, l.value as left_val, e.operator, e.right_operand, r.value as right_val,
  CASE e.operator
  WHEN '<' THEN
      (l.value < r.value)
  WHEN '=' THEN
      (l.value = r.value)
  WHEN '>' THEN
      (l.value = r.value)
  END as eval
FROM 
    expression as e
JOIN
    variable as l on e.left_operand = l.name
JOIN
    variable as r on e.right_operand = r.name

Here's a screenshot of my output: enter image description here

I also have a db-fiddle link for you to check out. https://www.db-fiddle.com/f/fdnJVSUQHS9Vep4uDSe5ZP/0

DonkeyKongII
  • 431
  • 2
  • 8