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

  • Home
  • SEARCH
  • 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 6769235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:10:08+00:00 2026-05-26T15:10:08+00:00

Trying to get a simple COUNT from a table that takes a couple of

  • 0

Trying to get a simple COUNT from a table that takes a couple of joins and some math. I can’t get it right. I keep running into “invalid use of group function.” In a similar query I get get all the data, just can’t get a count.

I have 4 tables.

CUSTOMERS
id  name  thegroup periodicity
1   Foo     3        4

GROUPS
id  group_name
3    New York

Each customer has a series of pickup dates.

COLLECTIONS
id  coll_date   customer_id
3   2011-09-07  1
3   2011-07-21  1

And a periods table which is the number of weeks between scheduled pickups.

PERIODS
id  duration
4   4

So a duration of 4 means pickup every 4 weeks, or 28 days.

I can calculate when the next pickup date is getting close: If they have a periodicity of 28 days, then when it has been 21 days since the last pickup it is time to flag it. In that case it would be 75% through its periodicity 21/28, or:

(ABS(DATEDIFF(MAX(collections.coll_date), NOW())) / (periods.duration *7))

If that result is greater than .75, then they are due for a pick up.
I can loop through customers and calculate that no problem.

My goal is:
I need to know how many customers in each group are due for pick up, re: COUNT(customers) WHERE (this calculation) > .75 GROUP BY group.id.

I have this so far, but can’t get it right.

SELECT COUNT(*), groups.id
FROM customers
INNER JOIN groups ON customers.thegroup = groups.id
INNER JOIN periods ON customers.periodicity = periods.id
LEFT JOIN collections ON customers.id = collections.rest_id
WHERE  status = 1 AND (ABS(DATEDIFF(MAX(collections.coll_date), NOW()))/(periods.duration*7)) > .75
GROUP BY groups.id
  • 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-26T15:10:08+00:00Added an answer on May 26, 2026 at 3:10 pm

    To list all groups with at least one customer having at least one pickup due according to your calculation and it shows how many customers have a pickup due use the following

    SELECT 
    C.THEGROUP,
    COUNT(DISTINCT C.ID) CNT
    FROM CUSTOMERS C 
    INNER JOIN PERIODS P ON C.PERIODICITY = P.ID
    LEFT OUTER JOIN (SELECT CL.REST_ID CUSTID, MAX (CL.COLL_DATE) MCOLLD FROM COLLECTIONS CL GROUP BY CL.REST_ID) X ON C.ID = X.CUSTID
    WHERE C.STATUS = 1 AND (ABS(DATEDIFF(X.MCOLLD, NOW()))/(P.DURATION*7)) > .75
    GROUP BY C.THEGROUP
    

    IF you want all groups listed including those not having any pickups use

    SELECT G.ID, IFNULL (A.CNT, 0) CNT FROM
    GROUPS G
    LEFT OUTER JOIN
    (
    SELECT 
    C.THEGROUP,
    COUNT(DISTINCT C.ID) CNT
    FROM CUSTOMERS C 
    INNER JOIN PERIODS P ON C.PERIODICITY = P.ID
    LEFT OUTER JOIN (SELECT CL.REST_ID CUSTID, MAX (CL.COLL_DATE) MCOLLD FROM COLLECTIONS CL GROUP BY CL.REST_ID) X ON C.ID = X.CUSTID
    WHERE C.STATUS = 1 AND (ABS(DATEDIFF(X.MCOLLD, NOW()))/(P.DURATION*7)) > .75
    GROUP BY C.THEGROUP
    ) A ON G.ID = A.THEGROUP
    ORDER BY G.ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get a simple demo started with ActiveMQ that will demonstrate
What I'm trying to achieve must be simple but I just can't get it
I have a jobs table, and am trying to get a count of jobs
I'm trying to get a count from a query with a group by and
I'm trying to get some simple data into a JsonStore, but it doesn't seem
I am trying to get simple jQuery to execute on my Content page with
I'm trying to get this simple PowerShell script working, but I think something is
I'm trying to get a simple search form working in my RoR site. I've
I'm trying to get this simple program to work on windows, but it crashes:
So i am trying to get a simple system working where i have an

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.