Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6738383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:21:43+00:00 2026-05-26T11:21:43+00:00

I have a query problem. I have a table of agents that do things.

  • 0

I have a query problem. I have a table of agents that do things. I keep track of the things they do in an events table. I want to keep my agents busy but not too busy, so I need a query that will return me a group of agents that have done no more that 10 events in the past 10 minutes and no more than 400 events in the past 24 hours. And from this pool of available agents I can choose one to give something to do

So my agent table looks something like:

Agent table
AgentID.       AgentName
1                    Bob
2                    Sue

Event Table

Event ID.     Agent ID.    Event Timestamp
1                  2        1319525462
2                  1        1319525462
3                  2        1319525462

Obviously these tables are just to give the form of the db. What I need generally and have not been able to figure out is how to select a group of agents from a join that returns a group of agents that have done no more than 10 events in the past 10 min and no more than 400 events in the past 24 hours. My actual tables are more complex, but I am just looking for a general principle on how to structure a query that would return the desired result. Thanks ahead of time for the help!

**UPDATE
building on Benoit’s answere I came up with this:

SELECT DISTINCT username FROM campaign_agents 
LEFT OUTER JOIN (SELECT count(event_index) myevents, field_event_agent_value FROM new_event WHERE field_event_time_value BETWEEN 1320206138 AND 1320292538 GROUP BY field_event_agent_value ) last_24_hours 
ON last_24_hours.field_event_agent_value = campaign_agents.username 
LEFT OUTER JOIN (SELECT count(event_index) myevents, field_event_agent_value FROM new_event WHERE field_event_time_value BETWEEN 1320291938 AND 1320292538 GROUP BY field_event_agent_value ) last_10_mins 
ON last_10_mins.field_event_agent_value = campaign_agents.username 
WHERE last_24_hours.myevents < 550 AND last_10_mins.myevents < 10

But it doesn’t get the agents in the campaign_agents table who haven’t done anything yet and are therefore not in the events table. Shouldn’t a LEFT OUTER JOIN include everything in the first table, campaign_agents, even if there are no matches to the second table? Do I need to put and OR statement after the where to somehow get them included?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:21:44+00:00Added an answer on May 26, 2026 at 11:21 am

    You can try:

    SELECT agent.agentid
      FROM agent
     INNER JOIN (SELECT count(eventid) events, agentid
                   FROM event
                  WHERE timestamp BETWEEN /* (now - 24 hours) */ AND /* now */ -- adapt this
                  GROUP BY agentid
                ) last_24_hours
        ON last_24_hours.agentid = agent.agentid
     INNER JOIN (SELECT count(eventid) events, agentid
                   FROM event
                  WHERE timestamp BETWEEN /* (now - 10 minutes) */ AND /* now */ -- adapt this
                  GROUP BY agentid
                ) last_10_mins
        ON last_10_mins.agentid = agent.agentid
    WHERE last_24_hours.events < 400
      AND last_10_mins.events < 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a performance problem on a query. First table is a Customer table
Got a problem with a query I'm trying to write. I have a table
I have the following query of linq to entities. The problem is that it
I have a tree / ancestor / query problem I'm not able to solve:
i would like to have a query that will solve my problem in native
I have a query that returns the highest-n values in a table and it
So My problem is this, I have a query that uses Mysql User-defined variable
I have a query that orders a table according to a particular field. The
I think this is a simple SQL query problem, but I am just not
I have a many-to-many query problem in Linq-to-SQL. I have a table named user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.