[pgsql-jp: 26307] Re: 記念日等の範囲検索

松嶋祥文 mats @ itboost.co.jp
2002年 6月 6日 (木) 13:24:24 JST


松嶋@ITBoost です。

| ユーザーに記念日等を登録させて、期間指定による条件絞り込みを行い該当者に
| メール配信をしたいのですが、よい方法が思いつきません。

プログラム側で処理する内容を少なくということで、

create table ttt ( ldate timestamp );

というテーブルに対して、

select * from 
 (select oid, (case when $from > $to and day <= $to 
                    then 10000 + day 
                    else day end) as vvv 
    from (select oid, date_part('month', ldate) * 100
                      + date_part('day', ldate) as day 
                 from shift) as xxx
 ) as yyy 
where case when $from > $to 
           then vvv <= $to_fixed and vvv >= $from 
           else vvv >= $from     and vvv <= $to    end;

みたいな感じでどうでしょう? 

実際にお使いのテーブルとはデータ型が違うようなので date_part は
使えないと思いますが、同様のことはできるはずです。

プログラムから用意する各変数は

  $from       〜から(月 * 100 + 日)
  $to         〜まで(月 * 100 + 日)
  $to_fixed   $to に 10000 をくわえたもの

です。数値に変換していますが、適当に年を当てはめるときに、
日付型だと閏年のことを考えるのが面倒だなとおもったからです。

以下、検証用です。
create table ttt ( ldate timestamp );
insert into ttt values ('1973-1-2');
insert into ttt values ('1974-3-2');
insert into ttt values ('1975-5-2');
insert into ttt values ('1976-7-2');
insert into ttt values ('1977-9-2');
insert into ttt values ('1978-11-2');
insert into ttt values ('1978-12-2');

$from      =   401
$to        =   901
$to_fixed  = 10901  (使われません)
   oid   | vvv
---------+------
 2002322 | 502
 2002323 | 702


$from      =  1101
$to        =   201
$to_fixed  = 10201
   oid   |  vvv
---------+-------
 2002320 | 10102
 2002325 |  1102
 2002326 |  1202

-- 
                                               ,-------------+
+---------------------------------------------'              |
| システム開発&エンジニアスクール アイティーブースト       |
| 取締役副社長   松嶋 祥文 [ mats @ itboost.co.jp ] ,-------------+
| +----------------------------------------------'              |
| | ★ Linux 関連の技術情報満載!    http://www.itboost.co.jp/  |
+-|■■■■■ Web アプリケーション開発、請け負います!■■■■■|
  +-------------------------------------------------------------+




pgsql-jp メーリングリストの案内