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.

【SQLServer】最大値のデータだけを更新したい【キーで絞り込めない場合】

Posted at

1 経緯

こんなテーブルがあった。

ID 値段
001 1000
001 3000
001 2000
001 1500

ここに最大フラグなるものを追加し、値段が最大のデータを知りたいと思った。
でもキーでは絞り込めない。いい感じにこうなってほしい。

ID 値段 最大フラグ
001 1000
001 3000 1
001 2000
001 1500

2 解決法

こう書いたら解決した。


UPDATE table
SET 最大フラグ = '1'
WHERE 値段 = ( SELECT TOP 1 値段
               FROM table
               ORDER BY 値段 DESC
             )

3 どう?

解決した?
こう見たらぱっと思いつきそうだけど詰まった上に調べても全然出てこなかったから、
同じように詰まった人の助けになったらいいなと思う。

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?