ASP,NET数据库问题SQL语法问题求大神指导,急
基本原理,数据库中记录着用户登录网站的时间,和离开的时间,现在需要统计同一天内各个时间段访问次数,如1-2小时段 ,2-3小时段,但在执行的时候提示select 语法错误,求大神帮忙解决主要是SQL语句的问题,万分感谢。弄了好久。。 ASP.NET 数据库 sql asp --------------------编程问答-------------------- select count(0) as Num, Datepart(hh,LoginTime) AS HourTime from tb_CounterInfo whereLoginTime>='" + M_Str_mindte + "' AND LoginTime < '" + M_Str_maxdate + "' AND Datepart(hh,LoginTime) = " + i + " GROUP BY Datepart(hh,LoginTime) "; --------------------编程问答-------------------- 用datediff函数计算日期间隔
http://www.w3school.com.cn/sql/func_datediff.asp
然后 select count(*) from xxxx where datediff(xxxx)=小时数 --------------------编程问答-------------------- 嗯,如果我是直接从数据库取系统的时候的话,跟从C#中有什么不同呢 --------------------编程问答-------------------- 同意! --------------------编程问答-------------------- SQL 写的好象同想要的不一样。 --------------------编程问答-------------------- oh ,看懂了,是要每天每小时的访问量吧??? 而且,,同样的IP也算么?
写了一下、、不喜勿喷
--测试数据
CREATE TABLE T_WebAccess
(
[id] INT PRIMARY KEY IDENTITY(1,1),
loginIp VARCHAR(10),
LoginTime DATETIME
)
INSERT INTO T_WebAccess VALUES('127.0.0.1',GETDATE()) --5times
INSERT INTO T_WebAccess VALUES('127.0.0.1','2013-03-07 00:27:09.000') --10times
INSERT INTO T_WebAccess VALUES('127.0.0.1','2013-03-07 14:27:09.000') --5times
INSERT INTO T_WebAccess VALUES('127.0.0.1','2013-03-07 23:27:09.000') --5times
INSERT INTO T_WebAccess VALUES('127.0.0.1','2013-03-08 00:27:09.000') --5times
GO
/*
结果:
id loginIp LoginTime
----------- ---------- -----------------------
1 127.0.0.1 2013-03-07 10:37:13.413
2 127.0.0.1 2013-03-07 10:37:13.570
3 127.0.0.1 2013-03-07 10:37:13.757
4 127.0.0.1 2013-03-07 10:37:13.930
5 127.0.0.1 2013-03-07 10:37:14.170
6 127.0.0.1 2013-03-07 00:27:09.000
7 127.0.0.1 2013-03-07 00:27:09.000
8 127.0.0.1 2013-03-07 00:27:09.000
9 127.0.0.1 2013-03-07 00:27:09.000
10 127.0.0.1 2013-03-07 00:27:09.000
11 127.0.0.1 2013-03-07 00:27:09.000
12 127.0.0.1 2013-03-07 00:27:09.000
13 127.0.0.1 2013-03-07 00:27:09.000
14 127.0.0.1 2013-03-07 00:27:09.000
15 127.0.0.1 2013-03-07 00:27:09.000
16 127.0.0.1 2013-03-07 14:27:09.000
17 127.0.0.1 2013-03-07 14:27:09.000
18 127.0.0.1 2013-03-07 14:27:09.000
19 127.0.0.1 2013-03-07 14:27:09.000
20 127.0.0.1 2013-03-07 14:27:09.000
21 127.0.0.1 2013-03-07 23:27:09.000
22 127.0.0.1 2013-03-07 23:27:09.000
23 127.0.0.1 2013-03-07 23:27:09.000
24 127.0.0.1 2013-03-07 23:27:09.000
25 127.0.0.1 2013-03-07 23:27:09.000
26 127.0.0.1 2013-03-08 00:27:09.000
27 127.0.0.1 2013-03-08 00:27:09.000
(27 row(s) affected)
*/
-- 个人爱好,,除了 SELECT * FROM 的查询语句,其它都喜欢写SP
CREATE PROC sp_GetAccessTimesByHour
@nowDate DATETIME --今天(或者要统计的时间)
AS
BEGIN
DECLARE @nowTime VARCHAR(10)
DECLARE @startTime VARCHAR(19) --开始时间
DECLARE @endTime VARCHAR(19) --结束时间
DECLARE @accessCount INT --每个时间段的访问量
DECLARE @accessTable TABLE (startTime DATETIME,endTime DATETIME,accessCount INT) --临时表变量,用来保存访问数据
SET @nowTime = CONVERT(VARCHAR(10),@nowDate,23)
DECLARE @hour INT --开始计时
SET @hour = 0
WHILE(@hour<24)
BEGIN
SET @endTime = @nowTime + ' '+CAST(@hour AS VARCHAR) +':59:59'
SET @startTime = @nowTime +' '+CAST(@hour AS VARCHAR) + ':00:00'
SELECT @accessCount = COUNT(*) FROM T_WebAccess WHERE LoginTime BETWEEN @startTime AND @endTime
INSERT INTO @accessTable VALUES(@startTime,@endTime,@accessCount)
SET @hour = @hour + 1 --计数 +1
--PRINT @startTime
--PRINT @endTime
END
SELECT * FROM @accessTable
END
GO
--执行
EXEC sp_GetAccessTimesByHour '2013-03-07 10:46:17'
GO
/*
结果
startTime endTime accessCount
----------------------- ----------------------- -----------
2013-03-07 00:00:00.000 2013-03-07 00:59:59.000 10
2013-03-07 01:00:00.000 2013-03-07 01:59:59.000 0
2013-03-07 02:00:00.000 2013-03-07 02:59:59.000 0
2013-03-07 03:00:00.000 2013-03-07 03:59:59.000 0
2013-03-07 04:00:00.000 2013-03-07 04:59:59.000 0
2013-03-07 05:00:00.000 2013-03-07 05:59:59.000 0
2013-03-07 06:00:00.000 2013-03-07 06:59:59.000 0
2013-03-07 07:00:00.000 2013-03-07 07:59:59.000 0
2013-03-07 08:00:00.000 2013-03-07 08:59:59.000 0
2013-03-07 09:00:00.000 2013-03-07 09:59:59.000 0
2013-03-07 10:00:00.000 2013-03-07 10:59:59.000 5
2013-03-07 11:00:00.000 2013-03-07 11:59:59.000 0
2013-03-07 12:00:00.000 2013-03-07 12:59:59.000 0
2013-03-07 13:00:00.000 2013-03-07 13:59:59.000 0
2013-03-07 14:00:00.000 2013-03-07 14:59:59.000 5
2013-03-07 15:00:00.000 2013-03-07 15:59:59.000 0
2013-03-07 16:00:00.000 2013-03-07 16:59:59.000 0
2013-03-07 17:00:00.000 2013-03-07 17:59:59.000 0
2013-03-07 18:00:00.000 2013-03-07 18:59:59.000 0
2013-03-07 19:00:00.000 2013-03-07 19:59:59.000 0
2013-03-07 20:00:00.000 2013-03-07 20:59:59.000 0
2013-03-07 21:00:00.000 2013-03-07 21:59:59.000 0
2013-03-07 22:00:00.000 2013-03-07 22:59:59.000 0
2013-03-07 23:00:00.000 2013-03-07 23:59:59.000 5
(24 row(s) affected)
*/
补充:.NET技术 , ASP.NET