在列中给定值之间检索记录

时间:2019-01-23 12:39:39

标签: sql oracle

请问您如何了解如何检索列中具有特定值的记录。

例如。我有一个包含几列的表,其中之一是“帐单周期”。对于该表中的每个记录,“帐单周期”列中的值都只有“ 01”,“ 09”,“ 16”或“ 23”。

所以,我想应用这样的规则:

如果“账单周期”列的值为“ 01”,则从01-01-2019提取记录至31-01-2019。

如果“账单周期”列的值为“ 09”,则提取记录从09-01-2018到08-02-2019。

如果“账单周期”列的值为“ 16”,请从16-01-2019提取记录至15-02-2019。

如果“账单周期”列的值为“ 23”,则从2018年12月23日到2019年1月23日提取记录。

2 个答案:

答案 0 :(得分:0)

您可以使用or

where (bill_cycle = '01' and datecol >= date '2019-01-01' and datecol < date '2019-01-31') or
      (bill_cycle = '09' and datecol >= date '2018-01-09' and datecol < date '2019-02-08') or
      (bill_cycle = '16' and datecol >= date '2019-01-16' and datecol < date '2019-02-15') or
      (bill_cycle = '23' and datecol >= date '2019-12-23' and datecol < date '2019-01-23')

答案 1 :(得分:0)

就像这样构建您的WHERE子句:

WHERE
(bill_cycle = '01' and dt_column between to_date('01-01-2019,'dd-mm-yyyy') and to_date('31-01-2019,'dd-mm-yyyy'))
OR
(bill_cycle = '09' and dt_column between to_date('09-01-2018,'dd-mm-yyyy') and to_date('08-02-2019,'dd-mm-yyyy')) 
OR
etc. etc.