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 8835535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:16:54+00:00 2026-06-14T09:16:54+00:00

I’m in trouble with a mysql statement counting appointments for one day within a

  • 0

I’m in trouble with a mysql statement counting appointments for one day within a given time period. I’ve got a calendar table including starting and finishing column (type = DateTime). The following statement should count all appointments for November including overall appointments:

SELECT 
    COUNT('APPOINTMENTS') AS Count, 
    DATE(c.StartingDate) AS Datum 
FROM t_calendar c
    WHERE 
        c.GUID = 'blalblabla' AND
        ((DATE(c.StartingDate) <= DATE('2012-11-01 00:00:00')) AND (DATE(c.EndingDate) >= DATE('2012-11-30 23:59:59'))) OR
        ((DATE(c.StartingDate) >= DATE('2012-11-01 00:00:00')) AND (DATE(c.EndingDate) <= DATE('2012-11-30 23:59:59')))
    GROUP BY DATE(c.StartingDate)
    HAVING Count > 1

But how to include appointments that starts before a StartingDate and ends on the StartingDate?

e.g.

  • StartingDate = 2012-11-14 17:00:00, EndingDate = 2012-11-15 08:00:00
  • StartingDate = 2012-11-15 09:00:00, EndingDate = 2012-11-15 10:00:00
  • StartingDate = 2012-11-15 11:00:00, EndingDate = 2012-11-15 12:00:00

My statement returns a count of 2 for 15th of November. But that’s wrong because the first appointment is missing. How to include these appointments? What I am missing, UNION SELECT, JOIN, sub selection?


A possible solution?

SELECT 
    c1.GUID, COUNT('APPOINTMENTS') + COUNT(DISTINCT c2.ANYFIELD) AS Count, 
    DATE(c1.StartingDate) AS Datum,
    COUNT(DISTINCT c2.ANYFIELD)
FROM 
    t_calendar c1
LEFT JOIN
    t_calendar c2
        ON
            c2.ResourceGUID = c1.ResourceGUID AND
            (DATE(c2.EndingDate) = DATE(c1.StartingDate)) AND 
            (DATE(c2.StartingDate) < DATE(c1.StartingDate))
WHERE 
    ((DATE(c1.StartingDate) <= DATE('2012-11-01 00:00:00')) AND (DATE(c1.EndingDate) >= DATE('2012-11-30 23:59:59'))) OR
    ((DATE(c1.StartingDate) >= DATE('2012-11-01 00:00:00')) AND (DATE(c1.EndingDate) <= DATE('2012-11-30 23:59:59')))    
GROUP BY 
    c1.ResourceGUID,
    DATE(c1.StartingDate)
  • 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-06-14T09:16:56+00:00Added an answer on June 14, 2026 at 9:16 am

    First: Consolidate range checking

    First of all your two range where conditions can be replaced by a single one. And it also seems that you’re only counting appointments that either completely overlap target date range or are completely contained within. Partially overlapping ones aren’t included. Hence your question about appointments that end right on the range starting date.

    To make where clause easily understandable I’ll simplify it by using:

    • two variables to define target range:
      • rangeStart (in your case 1st Nov 2012)
      • rangeEnd (I’ll rather assume to 1st Dec 2012 00:00:00.00000)
    • won’t be converting datetime to dates only (using date function) the way that you did, but you can easily do that.

    With these in mind your where clause can be greatly simplified and covers all appointments for given range:

    ...
    where (c.StartingDate < rangeEnd) and (c.EndingDate >= rangeStart)
    ...
    

    This will search for all appointments that fall in target range and will cover all these appointment cases:

                               start          end
    target range               |==============|
    
    partial front       |---------|
    partial back                            |---------|
    total overlap          |---------------------|
    total containment               |-----|
    

    Partial front/back may also barely touch your target range (what you’ve been after).

    Second: Resolving the problem

    Why you’re missing the first record? Simply because of your having clause that only collects those groups that have more than 1 appointment starting on a given day: 15th Nov has two, but 14th has only one and is therefore excluded because Count = 1 and is not > 1.

    To answer your second question what am I missing is: you’re not missing anything, actually you have too much in your statement and needs to simplified.

    Try this statement instead that should return exactly what you’re after:

    select count(c.GUID) as Count,
           date(c.StartingDate) as Datum
    from t_calendar c
    where (c.GUID = 'blabla') and
          (c.StartingDate < str_to_date('2012-12-01', '%Y-%m-%d') and
          (c.EndingDate >= str_to_date('2012-11-01', '%Y-%m-%d'))
    group by date(c.StartingDate)
    

    I used str_to_date function to make string to date conversion more safe.

    I’m not really sure why you included having in your statement, because it’s not really needed. Unless your actual statement is more complex and you only included part that’s most relevant. In that case you’ll likely have to change it to:

    having Count > 0
    

    Getting appointment count per day in any given date range

    There are likely other ways as well but the most common way would be using a numbers or ?calendar* table that gives you the ability to break a range into individual points – days. They you have to join your appointments to this numbers table and provide results.

    I’ve created a SQLFiddle that does the trick. Here’s what it does…

    Suppose you have numbers table Num with numbers from 0 to x. And appointments table Cal with your records. Following script created these two tables and populates some data. Numbers are only up to 100 which is enough for 3 months worth of data.

    -- appointments
    create table Cal (
      Id int not null auto_increment primary key,
      StartDate datetime not null,
      EndDate datetime not null
    );
    
    -- create appointments
    insert Cal (StartDate, EndDate)
    values
      ('2012-10-15 08:00:00', '2012-10-20 16:00:00'),
      ('2012-10-25 08:00:00', '2012-11-01 03:00:00'),
      ('2012-11-01 12:00:00', '2012-11-01 15:00:00'),
      ('2012-11-15 10:00:00', '2012-11-16 10:00:00'),
      ('2012-11-20 08:00:00', '2012-11-30 08:00:00'),
      ('2012-11-30 22:00:00', '2012-12-05 00:00:00'),
      ('2012-12-01 05:00:00', '2012-12-10 12:00:00');
    
    -- numbers table
    create table Nums (
      Id int not null primary key
    );
    
    -- add 100 numbers
    insert into Nums
    select a.a + (10 * b.a)
    from (select 0 as a union all
          select 1 union all
          select 2 union all
          select 3 union all
          select 4 union all
          select 5 union all
          select 6 union all
          select 7 union all
          select 8 union all
          select 9) as a,
         (select 0 as a union all
          select 1 union all
          select 2 union all
          select 3 union all
          select 4 union all
          select 5 union all
          select 6 union all
          select 7 union all
          select 8 union all
          select 9) as b
    

    Now what you have to do now is

    1. Select a range of days which you do by selecting numbers from Num table and convert them to dates.
    2. Then join your appointments to those dates so that those appointments that fall on particular day are joined to that particular day
    3. Then just group all these appointments per each day and get results

    Here’s the code that does this:

    -- just in case so comparisons don't trip over
    set names 'latin1' collate latin1_general_ci;
    
    -- start and end target date range
    set @s := str_to_date('2012-11-01', '%Y-%m-%d');
    set @e := str_to_date('2012-12-01', '%Y-%m-%d');
    
    -- get appointment count per day within target range of days
    select adddate(@s, n.Id) as Day, count(c.Id) as Appointments
    from Nums n
      left join Cal c
      on ((date(c.StartDate) <= adddate(@s, n.Id)) and (date(c.EndDate) >= adddate(@s, n.Id)))
    where adddate(@s, n.Id) < @e
    group by Day;
    

    And this is the result of this rather simple select statement:

    |        DAY | APPOINTMENTS |
    -----------------------------
    | 2012-11-01 |            2 |
    | 2012-11-02 |            0 |
    | 2012-11-03 |            0 |
    | 2012-11-04 |            0 |
    | 2012-11-05 |            0 |
    | 2012-11-06 |            0 |
    | 2012-11-07 |            0 |
    | 2012-11-08 |            0 |
    | 2012-11-09 |            0 |
    | 2012-11-10 |            0 |
    | 2012-11-11 |            0 |
    | 2012-11-12 |            0 |
    | 2012-11-13 |            0 |
    | 2012-11-14 |            0 |
    | 2012-11-15 |            1 |
    | 2012-11-16 |            1 |
    | 2012-11-17 |            0 |
    | 2012-11-18 |            0 |
    | 2012-11-19 |            0 |
    | 2012-11-20 |            1 |
    | 2012-11-21 |            1 |
    | 2012-11-22 |            1 |
    | 2012-11-23 |            1 |
    | 2012-11-24 |            1 |
    | 2012-11-25 |            1 |
    | 2012-11-26 |            1 |
    | 2012-11-27 |            1 |
    | 2012-11-28 |            1 |
    | 2012-11-29 |            1 |
    | 2012-11-30 |            2 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
i got an object with contents of html markup in it, for example: string
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to create an if statement in PHP that prevents a single post

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.