2017-06-13 91 views
0

我在寻找确定客户采取行动的平均时间。我有2列,customer_id和Action_timestamp。SQL:从日期时间选择平均小时

的样本数据:

customer_id | Action_timestamp 
1   | 2016-11-21T21:56:49.0000000 
1 | 2016-12-07T00:19:34.0000000 
1 | 2016-11-21T21:43:19.0000000 
2 | 2016-11-21T21:53:39.0000000 
3 | 2016-11-21T21:57:33.0000000 
3 | 2016-10-01T16:24:10.0000000 

我想组由CUSTOMER_ID并找到日期戳的平均时间。平均时间应等于所有时间的总和(OK忽略分钟)每个客户ID出现在的行数除以

我在寻找返回:

1 | 14 
2 | 21 
3 | 18 
+3

用您正在使用的数据库标记您的问题。 –

+0

还定义了您的上下文中的平均时间。 –

+0

感谢您的提示 - 添加了两个。 – Lars929

回答

1

这应该工作:

select customer_id,avg(extract(hour from Action_timestamp)) from <table_name> group by customer_id; 
+0

@ Lars929如果解决了您的问题,请将答案标记为已接受。谢谢 – Yankee