How to get the total amount with the most recent hourly / daily / weekly / annual interval in track 3?

I have a table like the following -

enter image description here

Now I need to make a report on the total number of calculations for each hour, week, month and year. It may have a sum of 0, but should be included in the result. For example, I need the result as follows:

$hourlyResult = array(
'00:01' => '5',
'00:02' => '9',
'00:03' => '50',
'00:04' => '5',
..............
..............
'00:55' => '95',
'00:56' => '0',
'00:57' => '20',
'00:58' => '33',
'00:59' => '5',
);

$weeklyResult = array(
'SAT' => '500',
'SUN' => '300'
.............
.............
'FRI' => '700'
);

How can I create a request in cakephp 3? I got the following link but can't go this far.

WEEK GROUP WITH SQL

What I've done -

    $this->loadModel('Searches');   
    $searches = $this->Searches
        ->find('all')
        ->select(['created', 'count'])
        ->where('DATE(Searches.created) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)')
        ->group(WEEK(date))
        ->hydrate(false)
        ->toArray();
    pr($searches);
+4
source share
1 answer

Here is how you can do it.

Amount for the year

    $query = $this->Searches->find();
    $query = $this->Searches
        ->find()
        ->select([
            'total_count' => $query->func()->sum('count'),
            'year' => $query->func()->year(['created' => 'literal'])
        ])
        ->group(['year'])
        ->hydrate(false);

Or

    $query = $this->Searches
        ->find()
        ->select(['total_count' => 'SUM(count)', 'year' => 'YEAR(created)'])
        ->group(['year'])
        ->hydrate(false);

Amount for the day of the week

    $query = $this->Searches->find();
    $query = $this->Searches
        ->find()
        ->select([
            'total_count' => $query->func()->sum('count'),
            'day_of_week' => $query->func()->dayOfWeek('created')
        ])
        ->group(['day_of_week'])
        ->hydrate(false);

Or

    $query = $this->Searches
        ->find()
        ->select(['total_count' => 'SUM(count)', 'day_of_week' => 'DAYOFWEEK(created)'])
        ->group(['day_of_week'])
        ->hydrate(false);

. CakePHP > SQL MySQL.

+1

Source: https://habr.com/ru/post/1650695/


All Articles