oracle sqldeveloper - SQL to retrieve last 3 records if they match a certain criteria -


i'm looking following

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 

basically, want able pull records of account if last 3 transactions have been paid cash.

regarding data above, account 1001 returned last 3 transactions have been cash, 1002 , 1003 not @ least 1 of last 3 transactions card payments.

you didn't tag rdbms, ansi-sql can use row_number() :

select tr.* tbl_transaction tr inner join(     select t.acc_id (         select t.acc_id,t.trans_type,                row_number() over(partition t.acc_id order t.trans_date desc) rnk         tbl_transaction t) tt     tt.rnk < 4 , t.trans_type = 'cash'     group tt.acc_id having count(*) = 3) s  on(t.acc_id = s.acc_id) 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -