Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
Consulta de PostgreSQL para obtener valor que ocurre más de una vez en 12 meses

Tengo la siguiente consulta para devolver la cantidad de usuarios que reservaron un vuelo al menos dos veces, pero necesito identificar aquellos que han reservado un vuelo más de una vez en el rango de 12 meses.

 SELECT COUNT(*) FROM sales WHERE customer in ( SELECT customer FROM sales GROUP BY customer HAVING COUNT(*) > 1 )
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Usarías funciones de ventana. El método más simple es lag() :

 select count(distinct customer) from (select s.*, lag(date) over (partition by customer order by date) as prev_date from sales s ) s where prev_date > s.date - interval '12 month';
over 4 years ago · Santiago Trujillo Report

0

Al costo de una auto-unión, la respuesta de @AdrianKlaver puede adaptarse a cualquier período de 12 meses.

 SELECT COUNT(DISTINCT customer) FROM (SELECT customer FROM sales s1 JOIN sales s2 ON s1.customer = s2.customer AND s1.ticket_id <> s2.ticket_id AND s2.date_field BETWEEN s1.date_field AND (s1.date_field + interval'1 year') GROUP BY customer HAVING COUNT(*) > 1) AS subquery;
over 4 years ago · Santiago Trujillo Report

0

Una puñalada con un campo de fecha inventado:

 SELECT COUNT(*) FROM sales WHERE customer in ( SELECT customer FROM sales WHERE date_field BETWEEN '01/01/2019' AND '12/31/2019' GROUP BY customer HAVING COUNT(*) > 1 )
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!