I have a table called visit_times in SQL like this
name time_stamp
Allen 2015-02-13 07:10:54
Allen 2015-02-13 07:10:58
Allen 2015-02-13 07:11:02
Mary 2015-02-17 10:45:33
Mary 2015-02-17 10:45:39
Mary 2015-02-17 10:45:43
...
I need to select names from "name" column for which all the row-consecutive differences (in second) in the "time_stamp" column equals a certain value. Using the LAG() command I have tried to code it up as follows
WITH cte AS
(
SELECT name,
DATEDIFF(second, LAG(time_stamp) OVER (PARTITION BY name ORDER BY time_stamp), time_stamp) AS visit_gap
FROM customer_transactions
)
SELECT cte.name
FROM cte
GROUP BY cte.name
HAVING MIN(cte.visit_gap) = 10 AND MAX(cte.visit_gap) = 4;
I expect to get the result as follows:
---------
| name |
---------
| Allen |
---------
But it outputs nothing! I get the error: in the pre-written template: Incorrect parameter count in the call to native function 'DATEDIFF'
I am not sure how to fix this. Any hints would be appreciated.