SQL如果匹配某个条件,则检索最后3条记录

时间:2016-06-28 10:21:15

标签: sql oracle-sqldeveloper

我正在寻找以下

TBL_TRANSACTION
(
  ACCOUNT ID
  TRANSACTION_DETAILS
  TRANSACTION_DATE
  TRANSACTION_TYPE
)

ACC_ID TRANS_DTLS TRANS_DATE TRANS_TYPE     
1001   Petrol     01-Sept-2015 Cash    
1001   Petrol     01-July-2015 Cash    
1001   Fruit      01-May-2015 Cash    
1001   Biscuits   01-Feb-2015 Cash    
1002   Cereal     01-Sept-2015 Cash    
1002   Soft Drinks 01-July-2015 Card    
1002   Water      01-May-2015 Cash    
1002   Water      01-Feb-2015 Card    
1003   Milk       01-Sept-2015 Card    
1003   Petrol     01-July-2015 Cash    
1003   Cereal     01-May-2015 Cash    
1003   Biscuits   01-Feb-2015 Cash

基本上,如果最后3笔交易全部由现金支付,我希望能够撤回账户记录。

关于上述数据,账户1001将被退回,因为最后3笔交易全部为现金,其中1002和1003不会,因为最近3笔交易中至少有1笔是信用卡付款。

3 个答案:

答案 0 :(得分:1)

您没有标记您的RDBMS,因此对于ANSI-SQL,您可以使用ROW_NUMBER()

SELECT tr.* FROM tbl_transaction tr
INNER JOIN(
    SELECT t.acc_id FROM (
        SELECT t.acc_id,t.trans_type,
               ROW_NUMBER() OVER(PARTITION BY t.acc_id ORDER BY t.trans_date DESC) as rnk
        FROM tbl_transaction t) tt
    WHERE tt.rnk < 4 and t.trans_type = 'Cash'
    GROUP BY tt.acc_id having count(*) = 3) s
 ON(t.acc_id = s.acc_id)

答案 1 :(得分:0)

with cte as
( select ACCOUNT_ID
       , TRANSACTION_DETAILS
       , TRANSACTION_DATE
       , TRANSACTION_TYPE 
       , row_number() over (partition by ACCOUNT_ID order by TRANSACTION_DATE desc) as rn 
  from TBL_TRANSACTION 
)

select cm.* 
from cte cm 
where cm.rn <= 3 
-- and cm.TRANSACTION_TYPE = 'cash' 
and cm.ACCOUNT_ID not in ( select cx.ACCOUNT_ID
                           from cte cx 
                           where rn <= 3 
                           and TRANSACTION_TYPE <> 'cash' ) 

答案 2 :(得分:0)

您可以使用row_number()获取最后三个帐户。您也可以使用row_number()使用不同的分区子句获取最后三个现金帐户。如果前三个值相同,那么最后三个是现金。

select t.*
from (select t.*,
             sum(case when seqnum = seqnum_type then 1 else 0 end) over (partition by acc_id) as num_in_sequence
      from (select t.*,
                   row_number() over (partition by acc_id order by transaction_date desc) as seqnum,
                   row_number() over (partition by acc_id, trans_type order by transaction_date desc) as seqnum_type
            from tbl_transaction t
           ) t
      where trans_type = 'cash'
     )
where num_in_sequence >= 3 and seqnum <= 3;

另一种方法比较最后三条记录的交易类型:

select t.*
from (select t.*,
             min(trans_type) over (partition by acc_d) as min_trans_type,
             max(trans_type) over (partition by acc_d) as max_trans_type
      from (select t.*,
                   row_number() over (partition by acc_id order by transaction_date desc) as seqnum
            from tbl_transaction t
           ) t
      where seqnum <= 3
     )
where min_trans_type = 'cash' and max_trans_type = 'cash';