[英]How do I get a Min and Max from certain ids in sql
I got a database with certain colums I am working with the adress and the time(unixtime) but I need to calculate from all colums with the same record(value) from the colum adres the time between the first and the last one 我有一个包含某些列的数据库,我正在处理地址和时间(unixtime),但我需要从所有列中获取相同记录(值)的列来计算第一个和最后一个之间的时间
So lets say adress x has 3 entries, then I want to sort the time from min to max. 因此,假设地址x有3个条目,那么我想将时间从最小到最大排序。 So I need to sort on adress, then time from min to max/max to min so I can calculate how long the person stayed there (time)
因此,我需要对地址进行排序,然后从最小到最大/最大到最大到最小的时间进行排序,以便我可以计算出该人在此停留的时间(时间)
I thin, you want group by
and the difference in times: 我瘦了,你想
group by
和时间的区别:
select address, min(from_unixtime(time)), max(from_uixtime(time)),
( max(from_unixtime(time)) - min(from_uixtime(time)) ) as diff
from sensordata1
group by address;
SELECT address
, MAX(FROM_Unixtime(time)) as myMaxTime
, MIN(FROM_Unixtime(time)) as myMinTime
, TIMEDIFF(MAX(FROM_Unixtime(time)), MIN(FROM_Unixtime(time)))
FROM sensordata1
GROUP BY address
If you do not want to return the entire dataset 如果您不想返回整个数据集
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.