나는 이렇게 학습한다/Algorithm & SQL

SQL Basics: Up and Down

daco2020 2022. 6. 21. 19:22
반응형

Given a table of random numbers as follows:

numbers table schema

  • id
  • number1
  • number2

Your job is to return table with similar structure and headings, where if the sum of a column is odd, the column shows the minimum value for that column, and when the sum is even, it shows the max value. You must use a case statement.

output table schema

  • number1
  • number2

 

 

 

Solution:

SELECT 
  CASE 
    WHEN MOD(SUM(number1),2) = 1 
    THEN MIN(number1) 
    ELSE MAX(number1) 
  END AS number1,
  CASE 
    WHEN MOD(SUM(number2),2) = 1 
    THEN MIN(number2) 
    ELSE MAX(number2) 
  END AS number2
FROM 
  numbers

 

 

 

Result:

number1 number2
208 86608

 

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

SQL: Padding Encryption  (0) 2022.06.23
Sum of angles  (0) 2022.06.22
GROCERY STORE: Inventory  (0) 2022.06.20
Best-Selling Books (SQL for Beginners #5)  (0) 2022.06.19
SQL Basics: Simple table totaling.  (0) 2022.06.18