LoginSignup
2
1

More than 5 years have passed since last update.

SQL コマンド集memo

Last updated at Posted at 2017-07-10

口座テーブル

口座番号 名義 種別 残高 更新日
0642191 アオキ ハルカ 1 364055 2013-03-13
1039410 キノシタ リョウジ 1 259017 2012-11-30
1239855 タカシナ ミツル 2 650977 NULL

残高が0より大きいデータを抽出
select * from 口座 where 残高 > 0

更新日が登録されていないデータを抽出
select * from 口座 where 更新日 is null

アオを含む名義のデータを抽出
select 名義 from 口座 where like '%アオ%'

更新日が2013年3月のデータを抽出
select * from 口座 where 更新日 between '2013-03-01' and '2013-03-31'

口座番号を降順に取得
select * from 口座 order by 口座番号 desc

更新日と残高を降順に11~20件のみ抽出。
ただし、残高が0円、または更新日の設定がないデータは除外する。
select 更新日, 残高 from 口座 where 残高 > 0 and 更新日 is not null order by 残高, 更新日 desc limit 10 offset 10

それぞれの残高から5000円ずつマイナスしてから0.3%足す。
update 口座 set 残高 = ( 残高 - 5000 ) * 1.003

2013年以前のデータを対象に、口座番号、更新日、通帳期限日を抽出。
通帳期限日は更新日の360日後とする。
select 口座番号, 更新日, 更新日 + 360 as 通帳期限日 from 口座 where 更新日 < '2013-01-01'

口座番号、名義、残高ランクを抽出。残高ランクは残高が30万未満をC、30万以上50万以内をB、それ以外をAとする。
select 口座番号, 名義,
case when 残高 < 300000 then 'C'
when 残高 >= 300000 and 残高 <= 500000 then 'B'
else 'A' end as 残高ランク from 口座

2
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
2
1