where trunc(ci.dat_creation)
between '01-may-2015' and '31-jul-2015'
and hbl.TRANS_DATE > '1-jun-2015'
这是我的查询where子句ci.datcreation告诉客户那些可能在jul之间的注册并获取那些已经在7月完成了他们的第一笔交易的客户的记录现在hbl.dat_creation获取6月以上的交易记录我希望我的查询检查交易日期应该是6月,交易在6月之前不存在我怎么能实现这个?
答案 0 :(得分:2)
假设ci.dat_creation和hbl.trans_date属于DATE或TIMESTAMP数据类型,您应该将date-as-strings显式转换为日期,例如:
where trunc(ci.dat_creation) between to_date('01/05/2015', 'dd/mm/yyyy') and to_date('31/07/2015', 'dd/mm/yyyy')
and hbl.TRANS_DATE > to_date('01/06/2015', 'dd/mm/yyyy')
另外,请记住DATE和TIMESTAMP也包含时间,如果您希望过滤结果包含ci.dat_creation在7月31日下午1点的行,那么您需要将where子句修改为:
where trunc(ci.dat_creation) >= to_date('01/05/2015', 'dd/mm/yyyy')
and trunc(ci.dat_creation) < to_date('31/07/2015', 'dd/mm/yyyy')
and hbl.TRANS_DATE > to_date('01/06/2015', 'dd/mm/yyyy')
关于您的问题,以下是我将如何处理的示例(您必须根据您的查询进行修改,因为您认为不适合包含整个问题它!):
with sample_data as (select 1 id, to_date('01/08/2015 12:39:28', 'dd/mm/yyyy hh24:mi:ss') dt from dual union all
select 1 id, to_date('15/08/2015 23:31:42', 'dd/mm/yyyy hh24:mi:ss') dt from dual union all
select 1 id, to_date('17/08/2015 08:29:36', 'dd/mm/yyyy hh24:mi:ss') dt from dual union all
select 2 id, to_date('16/08/2015 10:43:17', 'dd/mm/yyyy hh24:mi:ss') dt from dual union all
select 2 id, to_date('17/08/2015 17:51:11', 'dd/mm/yyyy hh24:mi:ss') dt from dual)
select id,
dt
from (select id,
dt,
count(case when dt < to_date('15/08/2015', 'dd/mm/yyyy') then 1 end) over (partition by id) num_dts_before
from sample_data)
where dt >= to_date('15/08/2015', 'dd/mm/yyyy')
and num_dts_before = 0;
ID DT
---------- ---------------------
2 16/08/2015 10:43:17
2 17/08/2015 17:51:11
显然,在您的代码中,您会对您要检查的日期进行参数化(例如,在上面的查询中,将to_date('15/08/2015', 'dd/mm/yyyy')
替换为p_date_check
)。