R
R
Roman Yamchuk2017-09-22 14:34:47
MySQL
Roman Yamchuk, 2017-09-22 14:34:47

How to group by value, capturing the date of the last entry?

there is a table with time and current temperature
correspondences cm_stat(ID, DATETIME, CURTIME)
Query

SELECT `cm_stat`.`CURTEMP`, `cm_stat`.`DATETIME`
FROM `cm_stat`
ORDER BY `DATETIME` ASC

At the exit:
Array
(
    [CURTEMP] => 27
    [DATETIME] => 2017-09-22 13:47:41
)
Array
(
    [CURTEMP] => 26
    [DATETIME] => 2017-09-22 13:47:41
)
Array
(
    [CURTEMP] => 27
    [DATETIME] => 2017-09-22 13:48:41
)
Array
(
    [CURTEMP] => 27
    [DATETIME] => 2017-09-22 13:48:41
)
Array
(
    [CURTEMP] => 27
    [DATETIME] => 2017-09-22 13:49:41
)
Array
(
    [CURTEMP] => 27
    [DATETIME] => 2017-09-22 13:49:41
)
Array
(
    [CURTEMP] => 27
    [DATETIME] => 2017-09-22 13:50:41
)
Array
(
    [CURTEMP] => 26
    [DATETIME] => 2017-09-22 13:50:41
)

It is necessary to group by ranges, fixing the date of the last modification.
Result:
Array
(
    [CURTEMP] => 27
    [DATETIME] => 2017-09-22 13:47:41
)
Array
(
    [CURTEMP] => 26
    [DATETIME] => 2017-09-22 13:47:41
)
Array
(
    [CURTEMP] => 27
    [DATETIME] => 2017-09-22 13:50:41
)
Array
(
    [CURTEMP] => 26
    [DATETIME] => 2017-09-22 13:50:41
)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2017-09-22
@Sanasol

just group and sort by date (you already have sorting in your query)
in the grouping selection, the most extreme date will be shown, taking into account sorting.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question