해커랭크 SQL문제_0716
Weather Observation Station2
문제:
Query the following two values from the STATION table:
The sum of all values in LAT_N rounded to a scale of decimal places.
The sum of all values in LONG_W rounded to a scale of decimal places.
Output Format
Your results must be in the form:
lat lon
where is the sum of all values in LAT_N and is the sum of all values in LONG_W. Both results must be rounded to a scale of 2 decimal places.
풀이:
LAT_N과 LONG_W의 합계를 구하고 이것을 반올림해서 소수점 아래 둘째자리 까지 출력하는 문제.
합계는 SUM 함수로 반올림은 ROUND 함수로 처리해주면 해결할 수 있다.
SELECT ROUND(SUM(LAT_N),2) AS lat, ROUND(SUM(LONG_W),2) AS lon FROM STATION; |
Weather Observation Station3
문제:
Query a list of CITY names from STATION for cities that have an even ID number.
Print the results in any order, but exclude duplicates from the answer.
The STATION table is described as follows:
풀이:
중복을 제거하고 CITY를 출력하되 ID가 짝수인 것들만 출력해야 한다.
중복을 제거하기 위해 DISTINCT를 사용하고,
짝수는 2로 나누었을 때 나머지가 0인 수들이니까 MOD 사용.
* 홀수는 MOD(ID,2) = 1 이런 식으로 판별해주면 될 것 같다.
SELECT DISTINCT CITY FROM STATION WHERE MOD(ID, 2) = 0; |