A
A
A1K02022-01-20 14:38:23
PostgreSQL
A1K0, 2022-01-20 14:38:23

How to select the date of the last transaction (so that this transaction is 7 days ago)?

help solve the problem plz.
There are services on which transactions take place, I need to select inactive services. A service is considered inactive if there were no transactions on it for a week. How can I choose these services? I tried this, but not that ... it gives me a range of 6 days before today, but I want the output to have only the date 7 days ago.
select
s.id as servie_id,
s.name as servie_name,
max(to_timestamp(s.created_at))::date as last_trans_date
from services
where to_timestamp(s.created_at)::date >= current_date - 6

group by 1,2
order by 1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Akina, 2022-01-20
@Akina

SELECT id AS servie_id,
       name AS servie_name,
       MAX(TO_TIMESTAMP(created_at))::DATE AS last_trans_date
FROM services
GROUP BY 1,2
HAVING last_trans_date <= CURRENT_DATE - INTERVAL '7 day'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question