0

假设所有产品在 1 年内到期

如何列出将在 2 个月后到期的产品?

table product
--------------------------------

food    | ManufacturedDate| 
--------------------------------
apple   | 12-nov-11  
orange  | 12-jan-12  
pear    | 12-jan-12  
milk    | 12-nov-11
mango   | 12-nov-11  

输出

orange  | 12-jan-12
pear    | 12-jan-12 
4

2 回答 2

1

你的数据说除了梨+橙之外的其他人今天到期,所以假设你想排除今天到期并包括那些在 2 个月内到期的人:

SQL> select food, manufacturedate, add_months(manufacturedate,12) expiry_date from product where add_months(manufacturedate, 12) <= add_months(trunc(sysdate), 2) and add_months(manufacturedate, 12) > trunc(sysdate);

FOOD        MANUFACTU EXPIRY_DA
--------------- --------- ---------
orange      12-JAN-12 12-JAN-13
pear        12-JAN-12 12-JAN-13

或更便于索引的放置方式(删除列侧的功能):

SQL> select food, manufacturedate, add_months(manufacturedate,12) expiry_date from product where manufacturedate <= add_months(trunc(sysdate), -10) and manufacturedate > add_months(trunc(sysdate), -12);

FOOD        MANUFACTU EXPIRY_DA
--------------- --------- ---------
orange      12-JAN-12 12-JAN-13
pear        12-JAN-12 12-JAN-13
于 2012-11-12T14:50:21.677 回答
0

尝试:

select * 
from product
where add_months( trunc(ManufacturedDate), 12 ) >= trunc(SYSDATE)
and add_months( trunc(ManufacturedDate), 12) <= add_months(trunc(SYSDATE), 2);
于 2012-11-12T14:45:27.437 回答