0

I need to compare some values from table, i.e. if less, greater and equal...

My query looks like:

  SELECT `t1`.`K1`
  IF (Sum(`t1`.`K1`) > Sum(`t2`.`K1`), 'Greater', 'Less') AS State
  FROM `t1`
  INNER JOIN `t2` ON `t1`.`Code` = `t2`.`Code`
  GROUP BY `t1`.`K1`

How to implement condition if values are equal?

1
  • Just btw, you are missing , after SELECT t1.K1... Commented Nov 22, 2013 at 11:14

2 Answers 2

3
SELECT `t1`.`K1`,
       case when Sum(`t1`.`K1`) = Sum(`t2`.`K1`) then 'Equal'
            when Sum(`t1`.`K1`) > Sum(`t2`.`K1`) then 'Greater'
            else 'Less'
       end AS State
FROM `t1`
INNER JOIN `t2` ON `t1`.`Code` = `t2`.`Code`
GROUP BY `t1`.`K1`
Sign up to request clarification or add additional context in comments.

Comments

1
SELECT `t1`.`K1`,
IF (Sum(`t1`.`K1`) = Sum(`t2`.`K1`), 'Equal', IF (Sum(`t1`.`K1`) > Sum(`t2`.`K1`), 'Greater', 'Less')) AS State
FROM `t1`
INNER JOIN `t2` ON `t1`.`Code` = `t2`.`Code`
GROUP BY `t1`.`K1`

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.