开发者

Help with statistical TSQL query

I have few tables that gather data for internal use but i want to write all data to other table from which i can easily query statistical data without joins, group by and other mess. This select insert query will be run as maintenance plan each 10 minutes.

I have 3 tables with data

Visitors (id int, siteID int, date smalldatetime, isRead bit, isActive bit)
VisitorLastLocation (id int, visitorID int, siteID int, durationInSeconds int)
Visitor开发者_C百科Locations(visitorLastLocationID int)

new item is added to Visitors table each time unique visitor comes to certain site, same goes for VisitorLastLocation. Next visit of already existing Visitor adds previous location to VisitorLocations and updates VisitorLastLocation table.

I need to fill this table each 10 minutes.

VisitorStatistics(id int, siteID int, visits int, pageViews int, isReadNumber int, isActiveNumber int, durationSum int, date smalldatetime)

So if you get me right i need to group by each selection by raw date (ie. 2/2/11 13:30 and not 2/2/11 13:31) and by siteID.

How would you advice to solve this, full solution will be very helpful...

thanks


From your description, all you are missing is the code to round a DATETIME to the lower 30 minute interval. DATEDIFF and DATEADD should help you here...

GROUP BY
  DATEADD(MINUTE, DATEDIFF(MINUTE, 0, [date]) - (DATEDIFF(MINUTE, 0, [date]) % 30), 0),
  siteID
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜