0

I have a field transaction_date which is varchar2, I want to make a search on that field and get user_id of that user who is not active for the past 6 months

I tried with below query but its not giving correct answer, my query is:

select distinct(user_id) 
from DAILYBOOKINGREPORT2 
where TRANSACTION_DATE  NOT BETWEEN to_date('31-MAY-2017') AND to_date('31-DEC-2016');
Bugs
  • 4,491
  • 9
  • 32
  • 41
bharath varma
  • 85
  • 1
  • 8

2 Answers2

1

Use GROUP BY and HAVING to find those users where the maximum transaction date was more than 6 months ago:

SELECT   user_id
FROM     DAILYBOOKINGREPORT2
GROUP BY user_id
HAVING   MAX( TRANSACTION_DATE ) < ADD_MONTHS( SYSDATE, -6 );
MT0
  • 143,790
  • 11
  • 59
  • 117
-1

You want to make use of the DATEADD function

SELECT 
       DISTINCT(user_id) 
 FROM `DAILYBOOKINGREPORT2` 
WHERE `TRANSACTION_DATE` <= DATEADD(mm, -6, GETDATE())

More information: https://www.w3schools.com/sql/func_dateadd.asp