我无法通过scr_SecuristLog加入#Temp。我该怎么做?
CREATE TABLE #Temp (VisitingCount int, [Time] int )
DECLARE @DateNow DATETIME,@i int,@Time int
set @DateNow='00:00'
set @i=1;
while(@i<48)
begin
set @DateNow = DATEADD(minute, 30, @DateNow)
set @Time = (datepart(hour,@DateNow)*60+datepart(minute,@DateNow))/30
insert into #Temp(VisitingCount,[Time]) values(0,@Time )
set @i=@i+1
end
select VisitingCount, [Time]
from #Temp as t
left outer join (
select count(page) as VisitingCount,
(datepart(hour,Date)*60+datepart(minute,Date))/30 as [Time]
from scr_SecuristLog
where Date between '2009-05-04' and '2009-05-05'
) as s
on t.VisitingCount = s.VisitingCount
and t.Time = s.Time
此代码给出错误:
Msg 8120,Level 16,State 1,Line 1
列'scr_SecuristLog.Date'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
Msg 8120,Level 16,State 1,Line 1
列'scr_SecuristLog.Date'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
Msg 209,Level 16,State 1,Line 1
不明确的列名称'VisitingCount'。
Msg 209,Level 16,State 1,Line 1
不明确的列名称'时间'。
答案 0 :(得分:2)
由于您没有提及特定错误,我猜测您的错误来自于您没有为选择值添加前缀的事实。
select t.VisitingCount, t.[Time]
修改强>
您的第二个错误应该由此小组解决。
select count(page) as VisitingCount,
(datepart(hour,Date)*60+datepart(minute,Date))/30 as [Time]
from scr_SecuristLog
where Date between '2009-05-04' and '2009-05-05'
GROUP BY (datepart(hour,Date)*60+datepart(minute,Date))/30
答案 1 :(得分:1)
我认为您需要在派生表scr_SecuristLog中添加GROUP BY,您需要按时间对其进行分组,因为您使用的是聚合函数计数。
答案 2 :(得分:1)
CREATE TABLE #Temp (
VisitingCount INT,
[Time] INT)
DECLARE @DateNow DATETIME,
@i INT,
@Time INT
SET @DateNow = '00:00'
SET @i = 1 ;
WHILE(@i < 48)
BEGIN
SET @DateNow = DATEADD(minute, 30, @DateNow)
SET @Time = (DATEPART(hour, @DateNow) * 60 + DATEPART(minute, @DateNow))
/ 30
INSERT INTO #Temp (VisitingCount, [Time])
VALUES (0, @Time)
SET @i = @i + 1
END
SELECT VisitingCount,
[Time]
FROM #Temp AS t
UNION
SELECT COUNT(page) AS VisitingCount,
(DATEPART(hour, Date) * 60 + DATEPART(minute, Date)) / 30 AS [Time]
FROM scr_SecuristLog
WHERE Date BETWEEN '2009-05-04' AND '2009-05-05'
GROUP BY Date
DROP TABLE #Temp
答案 3 :(得分:0)
好的,试试这个
CREATE TABLE #Temp (VisitingCount int, [Time] int )
DECLARE @DateNow DATETIME,@i int,@Time int
set @DateNow='00:00'
set @i=1;
while(@i<48)
begin
set @DateNow = DATEADD(minute, 30, @DateNow)
set @Time = (datepart(hour,@DateNow)*60+datepart(minute,@DateNow))/30
insert into #Temp(VisitingCount,[Time]) values(0,@Time )
set @i=@i+1
end
select t.VisitingCount, t.[Time]
from #Temp as t
left outer join (
select count(page) as VisitingCount,
(datepart(hour,Date)*60+datepart(minute,Date))/30 as [Time]
from scr_SecuristLog
where Date between '2009-05-04' and '2009-05-05'
GROUP BY scr_SecuristLog.Date
) as s
on t.VisitingCount = s.VisitingCount
and t.Time = s.Time
答案 4 :(得分:0)
您的内部选择(您正在加入)未正确聚合。你有一个计数和一列。这意味着您需要按SQL列进行分组才能正确理解它。
select t.VisitingCount, t.[Time]
from #Temp as t
left outer join (
select count(page) as VisitingCount,
(datepart(hour,Date)*60+datepart(minute,Date))/30 as [Time]
from scr_SecuristLog
where Date between '2009-05-04' and '2009-05-05'
GROUP BY [Date]
) as s
on t.VisitingCount = s.VisitingCount
and t.Time = s.Time
您可能实际需要的是按计算列进行分组,在这种情况下,您的分组应该是:
GROUP BY (datepart(hour,Date)*60+datepart(minute,Date))/30