简体   繁体   中英

How to calculate the time different between two columns(max value)?

The table look like this:

桌子看起来像这样

I want to calculate the time different between MAX(salinity) and MAX(Depth) on a day and stn_id = 'S1'. How can I do it?

SELECT  `r`.`date` ,  `r`.`time` ,  `r`.`salinity` 
FROM  `samlae` r
INNER JOIN 
(
    SELECT  `date` , MAX(  `salinity` ) AS  `maxsalinity` 
    FROM  `samlae` 
    GROUP BY  `date`
)t ON  `t`.`date` =  `r`.`date` 
AND  `t`.`maxsalinity` =  `r`.`salinity`

I would do it like this. There may be a simpler way depending on your exact requirement and the size of the original dataset

select max_values.date , (salinity.max_time - depth.max_time) as time_diff
from
    (
        select date, max(salinity) as max_salinity, max(depth) as max_depth
        from table
        group by 1
    ) max_values

inner join
    (
        select date, salinity, max(time) as max_time
        from table
        group by 1,2
    ) salinity
on max_values.date = salinity.date
    and max_values.max_salinity = salinity.salinity

inner join
    (
        select date, depth, max(time) as max_time
        from table
        group by 1,2
    ) depth
on max_values.date = depth.date
    and max_values.max_depth = depth.depth

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM