有一大表,表结构大致如下:
id --编号
l_date --交易日期
money  --金额
...
一个编号一天可以发生多笔,这表一天大致有五六万条记录,
我如何做到查找当天的所有记录在上三个月是否有交易?
但如果用普通的打开游标的方法进行判断实在速度太慢,请提供一快速的做法,最好有实例!

解决方案 »

  1.   

    select * from table t where l_date>trunc(sysdate) and l_date<trunc(sysdate)+1 and exists(
    select * from table where months_between(sysdate,l_date)<=3 and id=t.id)id和l_date上建索引
    如果还慢的话把months_between(sysdate,l_date)<=3 条件改一下
      

  2.   

    try:select * from tb
    where exists( select 1 from tb t where tb.id=t.id and l_date<trunc(sysdate) and l_date>add_months(sysdate,-3));
      

  3.   

    方法一,增加一个字段,用于记录最后交易日期 l_latestDate,则sql执行最快:
    select * from table where trunc(l_date) = trunc(sysdate) and months_between(sysdate,l_latestDate)<=3方法二,
    同意楼上二位的方法
      

  4.   

    to  olivenan:
    你说的正确,确实是要处理沉默用户
    to wuxking:
    我建的是分区表和索引,这表查询量很大,数据已经有三年多的了.