繁体   English   中英

如何使用Union方法或左外连接?

[英]How to use Union method or left outer join?

我无法通过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

此代码给出错误:



消息8120,级别16,状态1,行1列'scr_SecuristLog.Date'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
消息8120,级别16,状态1,行1列'scr_SecuristLog.Date'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
消息209,级别16,状态1,行1不明确的列名称'VisitingCount'。
消息209,级别16,状态1,行1不明确的列名称“时间”。

由于您没有提到特定错误,我猜您的错误来自于您没有为选择值添加前缀的事实。

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

我认为您需要在派生表scr_SecuristLog中添加GROUP BY,您需要按时间对其进行分组,因为您使用的是聚合函数计数。


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

好的,试试吧

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

您的内部选择(您正在加入)不会正确聚合。 你有一个计数和一列。 这意味着您需要按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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM