それはあなたがするとき
The offer starts within the search interval, OR
(the offer starts *before* search interval and
start time of the offer >= start time of the search range
as long as repeating weekdays match)
選択するように私は私がその権利を持って、本当に、本当にわからないですね。だから私は問題を別の見方を取るつもりです。私はUnixエポックの代わりにSQLタイムスタンプを使うつもりだから、誰もが何が起こっているのかを見ることができます。
また、私は終わりの夜中に提供を終了すると仮定しています。今は私にとって意味のある他の仮定はないようです。
create table offerings (
offer_id integer not null,
offer_start timestamp not null,
offer_end timestamp not null check (offer_end > offer_start),
primary key (offer_id, offer_start)
);
-- Offer 1 starts 2012-01-10 10:00, ends 2012-01-10 23:59, repeats every Tue.
insert into offerings values (1, '2012-01-10 10:00', '2012-01-10 23:59');
insert into offerings values (1, '2012-01-17 10:00', '2012-01-17 23:59');
insert into offerings values (1, '2012-01-24 10:00', '2012-01-24 23:59');
insert into offerings values (1, '2012-01-31 10:00', '2012-01-31 23:59');
-- Offer 2 starts 2012-01-10 08:00, ends 2012-01-10 23:59, repeats every Tue.
insert into offerings values (2, '2012-01-10 08:00', '2012-01-10 23:59');
insert into offerings values (2, '2012-01-17 08:00', '2012-01-17 23:59');
insert into offerings values (2, '2012-01-24 08:00', '2012-01-24 23:59');
insert into offerings values (2, '2012-01-31 08:00', '2012-01-31 23:59');
-- Offer 3 starts 2012-01-11 12:00, ends 2012-01-11 23:59, repeats every Wed, Thu, Fri.
insert into offerings values (3, '2012-01-11 12:00', '2012-01-11 23:59');
insert into offerings values (3, '2012-01-12 12:00', '2012-01-12 23:59');
insert into offerings values (3, '2012-01-13 12:00', '2012-01-13 23:59');
insert into offerings values (3, '2012-01-18 12:00', '2012-01-18 23:59');
insert into offerings values (3, '2012-01-19 12:00', '2012-01-19 23:59');
insert into offerings values (3, '2012-01-20 12:00', '2012-01-20 23:59');
insert into offerings values (3, '2012-01-25 12:00', '2012-01-25 23:59');
insert into offerings values (3, '2012-01-26 12:00', '2012-01-26 23:59');
insert into offerings values (3, '2012-01-27 12:00', '2012-01-27 23:59');
ここで選択は簡単です。
select *
from offerings
where offer_start >= '2012-01-10 09:00'
and offer_end <= '2012-01-11 11:00';
offer_id offer_start offer_end
--
1 2012-01-10 10:00:00 2012-01-10 23:59:00
したがって、このオファリング表と同じ結果を戻すビューを作成できる場合は、そのビューを問い合せるだけです。
ここには3つのレッスンがあります。
- あなたを混乱させてはいけません。あなたが必要とするものを探してください。
- イースターの問題を忘れないでください。
- を提供テーブルにそれを簡素化した後(あなたが何かがあなたにべきを意味するものではありません。計算できるという理由だけで)、私はまだない私は基準を理解すると確信しています。コメントと修正後の
その後、。 。 。
-- PostgreSQL
create table offerings (
offer_id integer not null,
offer_at timestamp not null,
primary key (offer_id, offer_at)
);
-- A little data for offer 1. Inserts for 2 and 3 are similar.
insert into offerings
select 1 offering_id, '2011-01-11 10:00'::timestamp + (n || ' days')::interval offer_at
from generate_series(0, 1000, 7) n
where '2011-01-11 10:00'::timestamp + (n || ' days')::interval < '2012-03-01';
クエリは、もう一度死んでしまう。 (そして、少しの運で、実際にこの時間を修正します。)これらのクエリの両方が唯一のオファー番号1
select *
from offerings
where offer_at between '2012-01-10 09:00' and '2012-01-10 11:00'
select *
from offerings
where offer_at between '2012-01-10 09:00' and '2012-01-11 11:00'
を返します。それらについての最もよい事はそれを基になるデータが正確であると仮定すると、それは明らかだ、ということですクエリは正しいことを実行しています。コードのトラブルシューティングよりもデータのトラブルシューティングがずっと簡単です。
オファーの終了時刻を保存しないでください。 2012-01-19から2012-01-25までのすべてのオファーが必要な場合は、2012-01-29以降に開始される各オファーはどれですか? (その期間は平日になるためです) –
私はそれを明確にするための例を追加しました。それ以上の質問については私に尋ねてください。 – Normalo
あなたの例では、年月日または平日のタイプミスですか? –