地層を剥がす

声を失った人間の発声練習です

『スッキリわかるSQL入門 第2版』ドリル(題材A・LEVEL5)の答え

『スッキリわかるSQL入門 第2版』のドリルを解き、ひたすら答えを載せていくシリーズです。今回は題材A・LEVEL5。SQL特有の書き方にも、だいぶ慣れてきました。

 

49.

select sum(残高) as 残高の合計, max(残高) as 最大, min(残高) as 最小, avg(残高) as 平均, count(*) as 登録されているデータ件数
from 口座

 

50.

select count(*) as 件数
from 口座
where 種別 != '1'
and 残高 >= 1000000
and 更新日< '2018-01-01'

 

51.

select count(*)-count(更新日) as 件数
from 口座

 

52.

select max(名義) as 名義の最大値, min(名義) as 名義の最小値
from 口座

 

53.

select max(更新日) as 更新日の最大値, min(更新日) as 更新日の最小値
from 口座

 

54.

select 種別, sum(残高) as 残高の合計, max(残高) as 最大, min(残高) as 最小, avg(残高) as 平均, count(*) as データ件数
from 口座
group by 種別
order by 種別

 

55.

select substring(口座番号, 7, 1) as 口座番号の下一桁, sum(残高) as 残高, count(*) as データ件数
from 口座
group by 口座番号の下一桁
order by 口座番号の下一桁

 

56.

select substring(coalesce(cast(更新日 as varchar), 'XXXX年'), 1,4)|| '年' as 年,
sum(残高) as 残高の合計, max(残高) as 最大, min(残高) as 最小, avg(残高) as 平均, count(*) as データ件数
from 口座
group by 年
order by 年

 

57.

select 種別, sum(残高) as 残高の合計, count(*) as データ件数
from 口座
group by 種別
having sum(残高) > 3000000
order by 種別 

 

58.

select substring(名義, 1, 1) as 名義, count(名義) as 件数, avg(length(replace(名義, ' ', ''))) as 名義文字数の平均
from 口座
group by substring(名義, 1, 1)
having count(名義) >= 10
or avg(length(replace(名義, ' ', ''))) > 5
order by substring(名義, 1, 1)

 

『スッキリわかるSQL入門 第2版』ドリル(題材A・LEVEL4)の答え

『スッキリわかるSQL入門 第2版』のドリルを解き、ひたすら答えを載せていくシリーズです。今回は題材A・LEVEL4。関数がまったく使いこなせていない……頑張るぞ!

 

34.

select 口座番号, 残高/1000 as 千円単位の残高
from 口座
where 残高 >= 1000000

 

35.

insert into 口座
values('0652281',  'タカギ ノブオ', '1', 100000+3000, '2018-04-01')

 insert into 口座
values('1026413', 'マツモト サワコ', '1', 300000+3000, '2018-04-02')

insert into 口座
values ('2239710', 'ササキ シゲノリ', '1', 1000000+3000, '2018-04-03')

 

36.

update 口座
set 残高 = (残高 - 3000) * 1.003
where 口座番号 = '0652281' or 口座番号 = '1026413' or 口座番号 = '2239710'

 

37.

select 口座番号, 更新日, 更新日 + 180 as 通帳期限日
from 口座
where 更新日 <= '2016-12-31'

 

38.

select 口座番号, concat('カ)'|| 名義) as 名義
from 口座
where 種別 = '3'

 

39.

select distinct 種別 as 種別コード,
case 種別 when '1' then '普通'
when '2' then '当座'
when '3' then '別段' end as 種別名
from 口座
order by 種別コード

 

40.

select 口座番号, 名義,
case when 残高 < 100000 then 'C'
when 残高 > 100000 and 残高 < 1000000 then 'B'
else 'A' end as 残高ランク
from 口座
order by 残高ランク

 

41.

select length(口座番号) as 口座番号の文字数, length(replace(名義, ' ', '')) as 名義の文字数, length(cast(残高 as varchar)) as 残高の文字数
from 口座

 

42.

select 名義
from 口座
where substring(名義, 1,5) like '%カワ%'

 

43.

select 残高
from 口座
where length(cast(残高 as varchar)) >= 4
and substring(cast(残高 as varchar), length(cast(残高 as varchar))-2, 3) = '000'

 

44.

select 口座番号, 残高, trunc(残高 * 1.002, 0) as 利息
from 口座
order by 残高 desc

 

45.

select 口座番号, 残高,
case when 残高 < 500000 then trunc(残高 * 1.0001,0)
when 残高 >= 500000 and 残高 < 2000000 then trunc(残高 * 1.0002,0)
when 残高 >= 2000000 then trunc(残高 * 1.0003,0) end as 残高別利息
from 口座
order by 残高別利息 desc, 口座番号

 

46.

insert into 口座
values('0351262','イトカワ ダイ', '2',635110,current_date)

 

insert into 口座
values('1015513', 'アキツ ジュンジ','1',88463,current_date)

 

insert into 口座
values('1739298', 'ホシノ サトミ','1',704610,current_date)

 

47.

select 口座番号, 名義, 種別, 残高,
substring(cast(更新日 as varchar), 1, 4) || '年' ||
substring(cast(更新日 as varchar), 6, 2) || '月' ||
substring(cast(更新日 as varchar), 9, 2) || '日' as 更新日
from 口座
where 更新日 >= '2018-01-01' 

 

48.

select coalesce(cast(更新日 as varchar), '設定なし') as 更新日
from 口座
order by 更新日

 

『スッキリわかるSQL入門 第2版』ドリル(題材A・LEVEL3)の答え

『スッキリわかるSQL入門 第2版』のドリルを解き、ひたすら答えを載せていくシリーズです。今回は題材A・LEVEL3。最後の33でつまずきました。

 

24.

select *
from 口座
order by 口座番号

 

25.

select distinct 名義
from 口座
order by 名義

 

26.

select *
from 口座
order by 残高 desc, 口座番号

 

27.

select 更新日
from 口座
where 更新日 is not null
order by 更新日
offset 0 rows
fetch next 10 rows only

 

28.

select 更新日,残高
from 口座
where 残高 <> 0
and 更新日 is not null
order by 残高, 更新日 desc
offset 10 rows
fetch next 10 rows only

 

29.

select 口座番号
from 口座
union
select 口座番号
from 廃止口座
order by 口座番号

 

30.

select 名義
from 口座
except
select 名義
from 廃止口座
order by 名義 desc

 

31.

select 名義
from 口座
intersect
select 名義
from 廃止口座

 

32.

select 口座番号, 残高
from 口座
where 残高 = 0
union
select 口座番号, 解約時残高
from 廃止口座
where 解約時残高 <> 0
order by 1

 

33.

select 口座番号,名義,'❍' as 区分
from 口座
union
select 口座番号,名義, '✕' as 区分
from 廃止口座
order by 名義

 

『スッキリわかるSQL入門 第2版』ドリル(題材A・LEVEL2)の答え

『スッキリわかるSQL入門 第2版』のドリルを解き、ひたすら答えを載せていくシリーズです。今回は題材A・LEVEL2。難易度的にはまだまだ平気です。かかってこーーーーい!

 

9.

select *
from 口座
where 口座番号 = '0037651'

 

10.

select *
from 口座
where 残高 > 0

 

11.

select *
from 口座
where 口座番号 < '1000000'

 

12.

select *
from 口座
where 更新日 <= '2017-12-31'

 

13.

select *
from 口座
where 残高 >= 1000000

 

14.

select *
from 口座
where 種別 <> '1'

 

15.

select *
from 口座
where 更新日 is null 

 

16.

select *
from 口座
where 名義 like '%ハシ%'

 

 17.

select *
from 口座
where 更新日 between '2018-01-01' and '2018-01-31'

 

18.

select *
from 口座
where 種別 in ('2','3')

 

19.

select *
from 口座
where 名義 in ('サカタ リョウヘイ', 'マツモト ミワコ', 'ハマダ サトシ')

 

20.

select *
from 口座
where 更新日 >= '2017-12-30'
and 更新日 <= '2018-01-04'

 

21.

select *
from 口座
where 残高 < 10000
and 更新日 is not null

 

22.

select *
from 口座
where 口座番号 like '2______'
or 名義 like 'エ__ %コ'

 

23.

口座テーブル:口座番号

取引テーブル:取引番号

取引事由テーブル:取引事由ID

『スッキリわかるSQL入門 第2版』ドリル(題材A・LEVEL1)の答え

『スッキリわかるSQL入門 第2版』のドリルを解き、ひたすら答えを載せていくシリーズです。今回は題材A・LEVEL1で最も難易度の低いクエリ。これくらいならお茶の子さいさいだよ!

 

1.

select 口座番号, 名義, 種別, 残高, 更新日
from 口座

2.

select 口座番号

from 口座

 

3.

select 口座番号, 残高
from 口座 

 

4.

select *
from 口座 

 

5.

update 口座
set 名義 = 'XXXXX'

 

6.

update 口座
set 残高 = 99999999, 更新日 = '2018-03-01'

7.

insert into 口座
values ('0642191', 'アオキ ハルカ', '1', 3640551, '2018-03-03'

insert into 口座
values ('1039410', 'キノシタ リュウジ', '1', 259017, '2017-11-30')  

insert into 口座
values ('1239855', 'タカシナ ミツル', '2', 6509773, NULL)

8.

delete from 口座

 

西暦を和暦に変換するGoogle日本語入力のユーザー辞書も作ったよ

先ほど和暦を西暦に変換するGoogle日本語入力のユーザー辞書を作成し、ブログで公開したところ、友人から「西暦を和暦に変換する需要のほうが多いのでは?」と指摘を受けました。それもそうだな~。というわけで、西暦→和暦バージョンも作ってみました。使い方は和暦→西暦バージョンと同じです。どっちも使うと年号無双になれるぞ! ぜひご利用を~! ↓

 

www.dropbox.com

和暦を西暦に変換するGoogle日本語入力のユーザー辞書を作ったよ

ネットで行政文書を見ていると、年号が和暦になっており、ついイラッとしがちです。そうだ、和暦をすぐに西暦に変換してくれるツールを作ろう! というわけで、Google日本語入力のユーザー辞書を作りました。Google日本語入力にこの辞書をインポートし、和暦を入力すれば、対応する西暦に変換してくれます。使い方は以下をどうぞ。

使い方

1.以下のユーザー辞書をローカルに落とします。

www.dropbox.com

 

2.落としたユーザー辞書をGoogle日本語入力にインポートします。やり方がわからない方は、以下の記事をご覧ください。

tipstour.net

3.西暦にしたい和暦をひらがな+数字で入力します。昭和22年の場合は「しょうわ22」と入れてみてください。すると、変換候補に西暦が出てきます。

f:id:tekitoeditor:20200214114445p:plain

 

謝辞

このユーザー辞書では、JCBさんが公開している以下の和暦西暦対照表をベースとして使わせていただきました。感謝です!

www.jcb.co.jp

 

このユーザー辞書でみなさまのイライラが少しでも減りますように!(落とした方はコメントなど残してくださると励みになってありがたいです🙏✨)

 

追記(2020/02/14 14:56)

西暦→和暦バージョンも作りました。こちらもよろしければご利用ください!

tekitoeditor.hatenadiary.com