Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jns
Starting Member
19 Posts |
Posted - 2002-08-13 : 13:49:18
|
| I have a table of information that tracks a userid and the date/time which they visit a site. What I need to be able to do is figure out how many people visited only once, two, three times, etc. The differenciating factor will be a gap in the time of four hours or more. More over, I need to compare from one year to another, month to month, etc. Any help would be appreciatedtableuid INTvdate DATETIMESample data1 2002-08-12 18:01:26.3901 2002-08-12 18:01:27.6071 2002-08-12 18:01:32.9501 2002-08-13 10:20:58.5771 2002-08-13 10:20:59.5001 2002-08-13 10:21:13.6071 2002-08-13 10:21:13.9201 2002-08-13 10:59:19.6401 2002-08-13 10:59:21.3901 2002-08-13 10:59:31.1871 2002-08-13 12:58:44.6401 2002-08-13 12:58:45.8271 2002-08-13 12:58:51.9201 2002-08-13 12:59:02.1071 2002-08-13 12:59:03.2001 2002-08-13 12:59:07.57710027 2002-07-16 18:03:33.64010027 2002-07-16 18:03:34.29310027 2002-07-16 18:03:34.42010027 2002-07-16 18:03:58.74710027 2002-07-16 18:04:12.95010027 2002-08-13 07:08:59.12310027 2002-08-13 07:09:00.37310027 2002-08-13 07:09:00.45010027 2002-08-13 07:09:32.43710027 2002-08-13 07:10:21.48010027 2002-08-13 07:12:17.65310079 2002-08-12 07:05:15.40310079 2002-08-12 07:05:15.85710079 2002-08-12 07:05:21.17010079 2002-08-12 07:05:45.42010079 2002-08-12 07:05:50.42010113 2002-07-16 15:26:02.09010113 2002-07-16 15:26:02.92010113 2002-08-10 13:29:57.76310113 2002-08-10 13:29:58.03010113 2002-08-10 13:29:58.03010113 2002-08-10 13:30:13.37310113 2002-08-10 13:30:13.45010113 2002-08-10 13:30:24.90310113 2002-08-10 13:30:38.50010118 2002-07-17 00:14:47.43310118 2002-07-17 00:14:47.78010118 2002-07-17 00:18:19.17010118 2002-07-17 00:18:27.32710118 2002-07-17 00:18:32.98310118 2002-07-17 00:22:55.78010118 2002-07-17 00:28:12.70010118 2002-07-17 00:28:17.07710118 2002-07-17 00:29:29.577 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-08-13 : 14:22:11
|
You didn't post your DDL or the INSERT statements required to insert the sample data, so I didn't test this solution (If you're too lazy to type it out, then so am I ), but I think this will work....create table #jns ( rownum int identity(1,1), uid int, vdate datetime, visitnumber int)insert into #jns (uid,vdate)select uid,vdate from <sourcetable>order by uid,vdatedeclare @lastuid int, @lastvdate datetime, @visitnumber intselect @lastuid = 0, @lastvdate = 0, @visitnumber = 1update #jnsset @visitnumber = visitnumber = case when @lastuid <> uid then 1 when datadiff(hh,@lastvdate,vdate) < 4 then @visitnumber else @visitnumber + 1 end, @lastvdate = vdate, @lastuid = uidselect a.[number of visits] count(*) as [the count]from ( select uid, max(visitnumber) as [number of visits] from #jns group by uid ) agroup by a.[number of visits]go Jay White{0} |
 |
|
|
1fred
Posting Yak Master
158 Posts |
Posted - 2002-08-13 : 14:31:13
|
| You can try converting your datetime data in float. The only rough part is to find what number is your right interval of time.select uid,count(uid) from tablewhere cast(vdate as float) > cast(getdate() as float)- 5 -- this is your number you'll have to find to fit it to 4hoursgroup by uidorder by count(uid) descto compare year and month use datepart function |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-08-13 : 14:47:31
|
1fred, one of us misread the problem.(And by "one of us", I mean YOU .... I think )jns, to use my script to compare from year to year, etc, you should probably store off the post-aggregated/churned data for each time-frame and do you comparisons from there....Jay White{0} |
 |
|
|
1fred
Posting Yak Master
158 Posts |
Posted - 2002-08-13 : 14:56:16
|
| I know my solution is not exactly what the guy asked for, I just try to give him some tools by the time I can take to answer. Btw while I was writing, your post was not there. |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-08-13 : 14:57:46
|
No worries, just giving you a hard time ...here you go... Jay White{0} |
 |
|
|
jns
Starting Member
19 Posts |
Posted - 2002-08-14 : 12:07:35
|
| Ok here's what I came up with to do total visits without using a temp table or cursorSELECT t3.crmid, count(*) + 1 FROM(SELECT t1.crmid, t1.createdFROM activity t1, activity t2WHERE t1.crmid=t2.crmid AND datediff(hh,t1.created,(SELECT TOP 1 t2.created FROM activity t2 WHERE t1.crmid=t2.crmid AND t2.created > t1.created)) >= 4GROUP by t1.crmid, t1.createdHAVING datediff(hh,t1.created,(SELECT TOP 1 t2.created FROM activity t2 WHERE t1.crmid=t2.crmid AND t2.created > t1.created)) > 4) t3GROUP by t3.crmidORDER by count(*) DESCThe + 1 considers that the query only returns the next visit that is 4 hours greater then the previous and adds 1 to the count to account for the initial visit. From here I'm sure I can group by day, week, month, quarter, year.Does anyone see any flaws with this? |
 |
|
|
|
|
|
|
|