V
V
Vladimir Druzhaev2020-04-06 12:40:26
PHP
Vladimir Druzhaev, 2020-04-06 12:40:26

How to write data for each day in statistics - duplicate or skip?

Statistics are written for each day.
Structure:

date             | type   | num
2020-04-06 | rating | 384


There are about 40 events, and some events do not change daily.
Question - is it worth writing such data to the database for every day?

Example: rating type. Yesterday it was = 384, today it is also 384 - it turns out that I am scoring immutable data and violating the principles of DRY.

But if I write only the changed data, I don't understand how I can get a selection from the database.
SELECT * 
FROM `site_statistics` 
WHERE type = 'rating'
AND date = '2020-04-06'


- won't give me anything back. But there may be data for yesterday. How do I get the next descending date data in this case? if, for example, there was a record a week ago, how can a subquery find the last value of the type?

It is necessary to receive data for the current day in the statistics, but if they have not changed, then take the last value for the next few days when the record was made.

Or is it still not worth bothering and writing the same value to the database every day?

Then there might be:
2020-04-06 | rating | 384
2020-04-05 | rating | 384
2020-04-04 | rating | 384
2020-04-03 | rating | 384
2020-04-02 | rating | 384
2020-04-01 | rating | 384
2020-03-31 | rating | 384
2020-03-30 | rating | 360

Let me remind you that so far there are 40 events and there is a possibility that there will be more of them.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
green_goo, 2019-10-02
@green_goo

We get the element with the key $a from the array, put it in the output array.
We add to the output array all elements of the original array whose key does not match $a.

A
Arthur, 2019-10-02
@ar2rsoft

usort
In the callback, we simply compare the element with the desired value

T
ThunderCat, 2020-04-06
@OtshelnikFm

if yesterday it was 27 degrees and today it's 27, maybe meteorologists shouldn't record the temperature every day?
It is natural to write if these parameters relate to logging and building any statistics, and this does not apply to DRY in any way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question