LoginSignup
1
1

More than 1 year has passed since last update.

100日でSQLの達人になる@LeetCode! Day63 <MS SQL Serverでべき乗、POWER>

Posted at

612. Shortest Distance in a Plane (問題レベル: Medium)要課金

今日の問題はこれ。
複数の点座標間の最も近い距離を求めよという問題。

2点間の距離を総当りで求めることはすぐに分かったのだが、なかなかアクセプトされない。

SQL Server 間違い
SELECT TOP 1 ROUND(SQRT((a.x-b.x)^2 + (a.y-b.y)^2),2) AS shortest
FROM point2d a
INNER JOIN point2d b
ON a.x <> b.x OR a.y <> b.y
ORDER BY ROUND(SQRT((a.x-b.x)^2 + (a.y-b.y)^2),2) ASC

Runtime Error, An invalid floating point operation occurred.というエラーが出てしまいます。

ポイントはMS SQL Serverで、べき乗する場合には、^は使えない、POWERを使うだった。
最終的に提出したコードは下記。

SQL Server
SELECT TOP 1 ROUND(SQRT(POWER((a.x-b.x),2) + POWER((a.y-b.y),2)),2) AS shortest
  FROM point2d a
 INNER JOIN point2d b
    ON a.x <> b.x OR a.y <> b.y
 ORDER BY ROUND(SQRT(POWER((a.x-b.x),2) + POWER((a.y-b.y),2)),2) ASC
  • LeetCodeの問題は、MS SQL Serverで解いています。
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1