Versuchen Sie so etwas: (Ich habe dies unter Oracle gemacht, es sollte meistens woanders funktionieren. Die WITH-Klauseln dienen hauptsächlich dazu, nur Beispieldaten zu fälschen. Also nicht unbedingt notwendig.)
with w_date_list as ( -- just some sample input dates - these are from your IN list (note that you want to re-org them as a "table" not an IN list - there's ways of doing that if you need help with that step
select to_date('01-apr-2015','dd-mon-yyyy') cdate from dual union all
select to_date('02-apr-2015','dd-mon-yyyy') cdate from dual union all
select to_date('03-apr-2015','dd-mon-yyyy') cdate from dual union all
select to_date('04-apr-2015','dd-mon-yyyy') cdate from dual union all
select to_date('05-apr-2015','dd-mon-yyyy') cdate from dual
),
w_date_rng as ( -- re-organize them into ranges using LEAD analytic function
select cdate start_date,
nvl(lead(cdate) over (order by cdate), cdate + 1 ) end_date -- last one, just default to 1 day
from w_date_list
)
select *
from (select to_date('01-jan-2015 03:14:46','dd-mon-yyyy hh24:mi:ss') yourdate from dual union all
select to_date('01-mar-2015 03:14:46','dd-mon-yyyy hh24:mi:ss') yourdate from dual union all
select to_date('01-apr-2015 03:14:46','dd-mon-yyyy hh24:mi:ss') yourdate from dual union all
select to_date('01-apr-2015 10:14:46','dd-mon-yyyy hh24:mi:ss') yourdate from dual union all
select to_date('01-apr-2015 13:14:46','dd-mon-yyyy hh24:mi:ss') yourdate from dual union all
select to_date('02-apr-2015 03:14:46','dd-mon-yyyy hh24:mi:ss') yourdate from dual union all
select to_date('02-apr-2015 21:14:46','dd-mon-yyyy hh24:mi:ss') yourdate from dual union all
select to_date('04-apr-2015 03:14:46','dd-mon-yyyy hh24:mi:ss') yourdate from dual union all
select to_date('04-apr-2015 15:14:46','dd-mon-yyyy hh24:mi:ss') yourdate from dual union all
select to_date('04-apr-2015 15:14:46','dd-mon-yyyy hh24:mi:ss') yourdate from dual union all
select to_date('04-apr-2015 15:14:46','dd-mon-yyyy hh24:mi:ss') yourdate from dual union all
select to_date('05-apr-2015 08:14:46','dd-mon-yyyy hh24:mi:ss') yourdate from dual union all
select to_date('05-apr-2015 16:14:46','dd-mon-yyyy hh24:mi:ss') yourdate from dual
) table1 , -- this just some fake data for your "table1" table.
w_date_rng wd
where table1.yourdate between wd.start_date and wd.end_date -- join the two with range ... it'll use an index on "yourdate" if it exists
/
Ergebnisse:
YOURDATE START_DATE END_DATE
-------------------- -------------------- --------------------
01-apr-2015 03:14:46 01-apr-2015 00:00:00 02-apr-2015 00:00:00
01-apr-2015 10:14:46 01-apr-2015 00:00:00 02-apr-2015 00:00:00
01-apr-2015 13:14:46 01-apr-2015 00:00:00 02-apr-2015 00:00:00
02-apr-2015 03:14:46 02-apr-2015 00:00:00 03-apr-2015 00:00:00
02-apr-2015 21:14:46 02-apr-2015 00:00:00 03-apr-2015 00:00:00
04-apr-2015 03:14:46 04-apr-2015 00:00:00 05-apr-2015 00:00:00
04-apr-2015 15:14:46 04-apr-2015 00:00:00 05-apr-2015 00:00:00
04-apr-2015 15:14:46 04-apr-2015 00:00:00 05-apr-2015 00:00:00
04-apr-2015 15:14:46 04-apr-2015 00:00:00 05-apr-2015 00:00:00
05-apr-2015 08:14:46 05-apr-2015 00:00:00 06-apr-2015 00:00:00
05-apr-2015 16:14:46 05-apr-2015 00:00:00 06-apr-2015 00:00:00
11 rows selected.
Dies könnte "verallgemeinert" werden als:
select *
from table1 ,
( select cdate start_date,
nvl(lead(cdate) over (order by cdate), cdate + 1 ) end_date
from ( select to_date('01-apr-2015','dd-mon-yyyy') cdate from dual union all
select to_date('02-apr-2015','dd-mon-yyyy') cdate from dual union all
select to_date('03-apr-2015','dd-mon-yyyy') cdate from dual union all
select to_date('04-apr-2015','dd-mon-yyyy') cdate from dual union all
select to_date('05-apr-2015','dd-mon-yyyy') cdate from dual
) w_date_list
) wd
where table1.yourdate between wd.start_date and wd.end_date
/
Das sollte auf jeder Datenbank funktionieren ... MYSQL, Oracle, was auch immer.
Sie müssen nur den Datumslistenbereich eingeben - es ist am besten, ihn als eine andere Tabelle oder so einzugeben ...
Ich könnte zeigen, wie es geht Oracle, aber wahrscheinlich nicht besonders nützlich für Sie :) Könnte eine andere Frage dafür brauchen, wenn nötig.