0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

100日でSQLの達人になる@LeetCode! Day16 <case when or iif(条件,真,偽)>

Posted at

610. Triangle Judgement (問題レベル: Easy)要課金

今日の問題はこれ。
3辺の長さが与えられた時、三角形が成立するかどうかを判定するという問題。

多段階の条件分岐が必要な難問かと思ったが、orandでまとめればシンプルな問題。
SQLはベン図を思い浮かべよという教えを見たことがあったがまさにそのとおり。

こちらが最初に考えた回答。Solutionに提示してある回答とほぼ同じだった。

select x, y, z, 
case when (x < y+z) and (y < x+z) and (z < x+y) then 'Yes'
     else 'No' end as triangle
from triangle

Discussを覗くと、一番Voteを得ていた回答はif(条件,真,偽)を使った回答。
しかし、何度実行してもacceptされない。
調べたところMS SQL Serverではif(条件,真,偽)は使用できずに
iif(条件,真,偽)を使う必要があるということ。
(もちろんif 条件 begin 真 end else bein 偽 endも使える)

select x, y, z, 
iif(x < y+z and y < x+z and z < x+y,'Yes', 'No') as triangle
from triangle

これならば通った。

基本は汎用性の高いcase whenで良いが、iif文も短く記載できるので覚えておこう。

  • LeetCodeの問題は、MS SQL Serverで解いています。
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?