Three consecutive days
How can I find out if a person has called more than three times in three consecutive days? The call table just...
Continue Reading This Article
Enjoy this article as well as all of our content, including E-Guides, news, tips and more.
keeps note of the person who called and on what date.
This is just another example of one of the many fine short questions that occasionally get submitted to Ask The Expert. A short question is best because it allows for leeway, even creativity, in interpreting the requirements.
For instance, which three consecutive days? Let's arbitrarily choose the three days prior to today, because today's not over yet (and the person may yet still call). Furthermore, if the person called six times yesterday, but not at all in the two days before yesterday, does that count? Let's arbitrarily say that there must be at least one call per day.
Finally, let's note that there have to be more than three calls in total.
Here's the solution:
select person_name , count(*) as callcount from calls where calldate >= current_date - interval '3' day and calldate < current_date group by person_name having count(distinct calldate) = 3 and count(*) > 3
For More Information
- Dozens more answers to tough SQL questions from Rudy Limeback.
- The Best SQL Web Links: tips, tutorials, scripts, and more.
- Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, SQL Server, DB2, object-oriented and data warehousing gurus are waiting to answer your toughest questions.