Zakładając, że twoje daty zawsze mają składnik czasu o północy, to:
SELECT *
FROM your_table
WHERE your_date_column IN (
TRUNC( SYSDATE ),
TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY,
ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -12 ),
ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -24 ),
ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -36 ),
ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -48 )
);
(Musisz dodać 1 dzień przed skróceniem, aby dopasować wielkość liter, gdy dzisiaj jest 31 grudnia)
Prostym rozwiązaniem byłoby użycie TRUNC
w dniu.
SELECT *
FROM your_table
WHERE TRUNC( your_date_column ) IN (
TRUNC( SYSDATE ),
TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY,
ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -12 ),
ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -24 ),
ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -36 ),
ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -48 )
);
Ale wtedy nie możesz użyć indeksu w kolumnie i potrzebujesz indeksu opartego na funkcjach w TRUNC( your_date_column )
aby móc korzystać z indeksu.
Bardziej skomplikowane rozwiązanie, które może używać indeksów, to:
SELECT *
FROM your_table
WHERE ( your_date_column >= TRUNC( sysdate )
AND your_date_column < TRUNC( sysdate ) + INTERVAL '1' DAY
)
OR ( your_date_column >= TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY
AND your_date_column < TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' )
)
OR ( your_date_column >= ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -12 )
AND your_date_column < ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ), -12 )
)
OR ( your_date_column >= ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -12 )
AND your_date_column < ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ), -24 )
)
OR ( your_date_column >= ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -12 )
AND your_date_column < ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ), -36 )
)
OR ( your_date_column >= ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -12 )
AND your_date_column < ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ), -48 )
);