혜미의 개발 일지

[SQL] 해커랭크 MSSQL 소수점자르기와 0제거하기 본문

코딩테스트/sql

[SQL] 해커랭크 MSSQL 소수점자르기와 0제거하기

혜미 2022. 4. 27. 10:56
반응형

Query the sum of Northern Latitudes (LAT_N) from STATION having values greater than 38.7880  and less than 137.2345. Truncate your answer to 4 decimal places.

Input Format

답안

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

답안 :

SELECT
FORMAT(ROUND(sum(LAT_N),4,1),'####0.########')
FROM STATION
WHERE LAT_N > 38.7880
AND LAT_N < 137.2345


문제 해석 : 38.7880 보다 크고 137.2345 작은 값을 갖는 STATION의 북방 위도(LAT_N) 합계를 쿼리합니다. 4 소수점 이하 자릿수까지 답을 자릅니다.

 

소수점 자르기  ROUND 함수 이용

ROUND(  값 ,  4 , 1 )

반올림은 ROUND(  값 ,  반올림할 위치)

예시) ROUND(123.123 ,2)  이면 소수점 두번째 자리가 반올림 위치

ROUND(123.123 ,-1) 이면 정수에서 숫자일의 자리가 반올림 위치 

자르기는 ROUND(  값 ,  자를 위치, 1)

올림은 CEILING( 값)

내림은 FLOOR(값)

 

출처 : https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=hjdu21&logNo=220937018008 

 

0 제거하기 FORMAT 함수 이용

FORMAT( ,'####0.########')

주의할 점은 숫자의 최대 자리수와 포맷(# or 0)의 길이가 일치해야 한다. 만약 소수점 이하의 값의 길이보다 "#"의 길이가 짧으면 해당 위치에서 반올림하여 표시한다.

#은 수치값의 "0"을 제거하고, "0"은 해당 자리에 "0"을 표시한다. 위의 예제에서 첫째 자리에 "0"으로 지정한 이유는 정수값이 없으면 "0"으로 표시하고 이후 소수점을 표시한다.

 

Weather Observation Station 15 :

Query the Western Longitude (LONG_W) for the largest Northern Latitude (LAT_N) in STATION that is less than 137.2345 Round your answer to  4 decimal places.

Input Format

The STATION table is described as follows:

해커랭크 답안

 

Weather Observation Station 17 :

Query the Western Longitude (LONG_W)where the smallest Northern Latitude (LAT_N) in STATION is greater than 38.7780 Round your answer to 4 decimal places.

Input Format

The STATION table is described as follows:

해커랭크 답안

반응형
BIG
Comments