1
1

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.

期間の検索条件についてのお話。

Last updated at Posted at 2021-03-24

問題

突然ですが、問題です。

とあるシステムの利用期間を管理しているユーザマスタが存在するとします。

2021/4/1 ~ 2021/10/1 の期間で

このシステムを1日でも利用できるユーザを検索したい場合
※下の図で言うと、ユーザA ~ ユーザG までを検索結果として出力したい。
image.png
どのように検索条件を書くのが最適でしょうか?

アプローチ

このような場合、図にして考えると割と考えやすいですが
面倒臭がって頭の中だけで考えて結論を出すと、結構ゴチャゴチャした条件を書きがちな気がします。(私だけか?)

地道にアプローチしていくと、
ユーザAが該当するにはユーザAの利用期間が2021/4/1 ~ 2021/10/1 内にあればいい。
でもこの条件だとユーザAとユーザDしか該当しないな。。。
じゃあユーザ利用期間(From)が2021/4/1 ~ 2021/10/1、またはユーザ利用期間(To)が2021/4/1 ~ 2021/10/1 にすれば。。。ユーザEが該当しない!
といった具合になります。
(私だけか?)

答え

実はユーザF と ユーザG を検索する条件だけを書けば、ユーザA ~ ユーザG が検索条件に該当します。

よって、
2021/4/1 <= ユーザ.利用期間(To) かつ ユーザ.利用期間(From) <= 2021/10/1

が答えになります。

おわりに

備忘録代わりです。

1
1
2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?