sql server - SQL Query AVG Date Time In same Table Column -
i’m trying make query returns difference of days average of days in period of time. situation need max date status 2 , max date status 3 request , how time user spend on period of time
so far query have right mas , min , difference between days not max of status 2 , max of status 3
query have far:
select distinct t1.user, t1.request, min(t1.time) mindate, max(t1.time) maxdate, datediff(day, min(t1.time), max(t1.time)) [hst_log] t1 t1.request = 146800 group t1.request, t1.user order t1.user, max(t1.time) desc
example table:
------------------------------- user | request | status | time ------------------------------- user 1 | 2 | 1 | 6/1/15 3:25 pm user 2 | 1 | 1 | 2/1/15 3:24 pm user 2 | 3 | 1 | 2/1/15 3:24 pm user 1 | 4 | 1 | 5/10/15 3:18 pm user 3 | 3 | 2 | 5/4/15 2:36 pm user 2 | 2 | 2 | 6/4/15 2:34 pm user 3 | 2 | 3 | 6/10/15 5:51 pm user 1 | 1 | 2 | 5/1/15 5:49 pm user 3 | 4 | 2 | 5/16/15 2:39 pm user 2 | 4 | 2 | 5/17/15 2:32 pm user 2 | 3 | 2 | 4/6/15 2:22 pm user 2 | 3 | 3 | 4/7/15 2:06 pm -------------------------------
i appreciate help
you'll need use subqueries since groups min , max times different. 1 query pull min value status 2. pull max value status 3.
something this:
select mindt.[user], mindt.mintime, maxdt.maxtime, datediff(d,mindt.mintime, maxdt.maxtime) timespan (select t1.[user], t1.request, min(t1.time) mintime [hst_log] t1 t1.request = 146800 , t1.[status] = 2 group t1.request, t1.[user]) mindt inner join (select t1.[user], t1.request, max(t1.time) maxtime [hst_log] t1 t1.[status] = 3 group t1.request, t1.[user]) maxdt on mindt.[user] = maxdt.[user] , mindt.request = maxdt.request
Comments
Post a Comment