简体   繁体   中英

Rounded to 4 decimal places, but output shows 4 decimal places + 4 0s in MS SQL Server

I am working on Weather Observation Station 17 in HackerRank. 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.

Table: STATION Fields: ID, CITY, STATE, LAT_N, LONG_W where LAT_N is the northern latitude and LONG_W is the western longitude.

My code:

SELECT ROUND(LONG_W,4,0) AS low
FROM STATION
WHERE LAT_N = (SELECT MIN(LAT_N) FROM STATION WHERE LAT_N > 38.7780);

Output: 70.13780000 The answer is wrong. I looked up this question online and the code looks the same in other answers. I am using MS SQL Server. The same code works fine on MySQL

The ROUND function will return the same datatype, precision and scale as the input:

select round(1.10045001, 4); -- 1.10050000
select round(1.10055001, 4); -- 1.10060000

You need CAST(... AS DECIMAL(..., 4)) to generate a decimal with exactly 4 digits. This function will round the value using same algorithm as ROUND during conversion:

select cast(1.10045001 as decimal(18, 4)); -- 1.1005
select cast(1.10055001 as decimal(18, 4)); -- 1.1006

I just wouldn't use ROUND :

SELECT CONVERT(decimal(12,4), LONG_W) AS low
FROM STATION
WHERE LAT_N = (SELECT MIN(LAT_N) FROM STATION WHERE LAT_N > 38.7780);

Also more efficient:

SELECT TOP (1) CONVERT(decimal(12,4), LONG_W) AS low
FROM STATION
WHERE LAT_N > 38.7780
ORDER BY LAT_N;

For MS SQL

SELECT CAST(LONG_W AS DECIMAL(10, 4))
FROM STATION
WHERE LAT_N = ( SELECT MIN(LAT_N) FROM STATION WHERE LAT_N > 38.7780);

MS SQL SERVER :

SELECT TOP 1 CAST(LONG_W AS DECIMAL(10,4)) FROM STATION WHERE LAT_N > 38.7780 ORDER BY LAT_N ASC

SELECT FORMAT(round(min(LAT_N),4),'F4')
FROM STATION
WHERE LAT_N > 38.7780;


Your Output (stdout)
38.8526

actually the question is little bit confusing but still it have some meaning which i understand after getting the right answer.

the condition for LAT_N is confusing, it means that *** "LAT_N is smallest one and the same time LAT_N is greater than 38.7780" ***

select round(LONG_W,4) from STATION where LAT_N = (SELECT MIN(LAT_N) FROM STATION WHERE LAT_N > 38.7780);

stdout: 70.1378

This should work. There are several lat_n values greater than 38.7780. However, we want the smallest one of them, and the corresponding long_w. So, to get the smallest lat_n, order the result by lat_n and set the limit to 1. This will pick up the smallest lat_n value.

SELECT ROUND(long_w, 4)
FROM station
WHERE lat_n > 38.7780
ORDER BY lat_n
LIMIT 1;
SELECT CAST(ROUND(LONG_W,4) AS DECIMAL(10, 4)) 
FROM STATION WHERE LAT_N = ( SELECT MIN(LAT_N) FROM STATION WHERE LAT_N > 38.7780);

this will work..

My Code works successfully on MYSQL

select round (LONG_W,4)
from STATION
where LAT_N>38.7780
order by LAT_N asc
limit 1;

this the correct answer for this Q in MYSQL

select round (LAT_N,4) from STATION where LAT_N>38.7780 order by LAT_N asc limit 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM