Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>In my table, all the begin_date and end_date values are round numbers like 2011-04-11 20:00:00 or 2011-04-11 14:30:00</p> </blockquote> <p>Unless you also have times like <code>2011-04-11 14:15:00</code> your desired result with steps of 30 minutes doesn't make much sense, unless you want to count some reservations two times.</p> <p>Apart from that in MySQL it would be something like this:</p> <pre><code>SELECT DATE(begin_time) AS `Date`, CASE WHEN TIME(begin_time) &gt;= '00:00:00' AND TIME(begin_time) &lt; '01:00:00' THEN '00:00:00 - 01:00:00' WHEN TIME(begin_time) &gt;= '01:00:00' AND TIME(begin_time) &lt; '02:00:00' THEN '01:00:00 - 02:00:00' /*...and so on...*/ END AS `TimeSlot`, COUNT(id) as reservations FROM reservation WHERE begin_time &gt; '" . $from . "' AND end_time &lt; '" . $until . "' GROUP BY `Date`, `TimeSlot` </code></pre> <p>I made it time slots of 1 hour and added the date to be grouped on.</p> <p>Actually, silly me, now that I think about it, when you have time slots of exactly one hour, you can also do it like this:</p> <pre><code>SELECT DATE(begin_time) AS `Date`, HOUR(begin_time) AS `TimeSlot`, COUNT(id) as reservations FROM reservation WHERE begin_time &gt; '" . $from . "' AND end_time &lt; '" . $until . "' GROUP BY `Date`, `TimeSlot` </code></pre> <p><strong>UPDATE:</strong></p> <pre><code>SELECT CASE WHEN TIME(begin_time) BETWEEN '00:00:00' AND '01:00:00' OR TIME(end_time) BETWEEN '00:00:00' AND '01:00:00' THEN '00:00:00 - 01:00:00' WHEN TIME(begin_time) BETWEEN '01:00:00' AND '02:00:00' OR TIME(end_time) BETWEEN '01:00:00' AND '02:00:00' THEN '01:00:00 - 02:00:00' /*...and so on...*/ END AS `TimeSlot`, COUNT(id) as reservations FROM reservation WHERE begin_time &gt; '" . $from . "' AND end_time &lt; '" . $until . "' GROUP BY `TimeSlot` </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload