我有一个表,其中包含datatime和person identity,我想在其中找出每天的时间差异,并找出SQL中的平均值。
输入表,
+---------------------+----------+--------+
| Datetime | Identity | Type |
+---------------------+----------+--------+
| 28/08/2019 4:00:00 | ABC | Entry |
| 28/08/2019 14:00:00 | ABC | Exit |
| 29/08/2019 6:00:00 | ABC | Entry |
| 29/08/2019 6:10:00 | ABC | Exit |
| 30/08/2019 8:00:00 | ABC | Entry |
| 30/08/2019 17:00:00 | ABC | Exit |
+---------------------+----------+--------+是否可以使用SQL中的以下数据创建另一个表?
+------------+----------+-----------+
| Date | Identity | Time(Min) |
+------------+----------+-----------+
| 28/08/2019 | ABC | 600 |
| 29/08/2019 | ABC | 10 |
| 30/08/2019 | ABC | 540 |
+------------+----------+-----------+提前谢谢你。
发布于 2019-08-30 13:24:17
你可以试试下面的方法--它适用于mysql
select cast(datetime as date),
TIMESTAMPDIFF(MINUTE,min(case when Type='Entry' then datetime end),
max(case when Type='Exit' then datetime end))
from tablename
group by cast(datetime as date)或者对于sql server -
select cast(datetime as date),
datediff(mi,min(case when Type='Entry' then datetime end),
max(case when Type='Exit' then datetime end))
from tablename
group by cast(datetime as date)发布于 2019-08-30 14:51:58
在SQL Server中,我认为这应该能满足您的需求:
select cast([datetime] as date) as [Date]
, [Identity]
, datediff(minute, min([datetime]), max([datetime])) as [Time(Min)]
from table_name
group by cast([datetime] as date)
, [Identity]https://stackoverflow.com/questions/57720881
复制相似问题