How to use Union method or left outer join?

I cannot join #Temp with scr_SecuristLog. How can I do it?

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
      

These codes give an error:



Msg 8120, Level 16, State 1, Line 1 Column "scr_SecuristLog.Date" is not valid in the select list because it is not contained in either an aggregate function or a GROUP BY clause.
Msg 8120, Level 16, State 1, Line 1 Column "scr_SecuristLog.Date" is not valid in the select list because it is not contained in either an aggregate function or a GROUP BY clause.
Msg 209, Level 16, State 1, Line 1 Ambiguous column name "VisitingCount".
Msg 209, Level 16, State 1, Line 1 Ambiguous column name "Time".

+1


a source to share


5 answers


As you didn't mention the specific error, I'm guessing that your error comes from the fact that you are not prefixing your select values.

select t.VisitingCount, t.[Time]

      




Edit

Your second error should be resolved with this group.

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

      

+2


a source


I think you need to add GROUP BY to your scr_SecuristLog view, you need to group it by time because you are using aggregate count.



+1


a source



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



+1


a source


Ok try this

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

      

0


a source


Your inner choices (which you are joining) are not being aggregated properly. You have a counter and a column. This means you need to group the SQL column to understand correctly.

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

      

Actually you may need to group the calculated column, in which case your group should be:

GROUP BY (datepart(hour,Date)*60+datepart(minute,Date))/30

      

0


a source







All Articles