2009-05-12 11 views
0

Wie generiert man diese SQL-Abfrage?

"0" ist VisitingCount --- Numerischer Wert Datepart, zum Beispiel: 00: 00--1, 00: 30--2, 01: 00--2, 01: 30--3


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 @[email protected]+1 
    end 


select Sum(VisitingCount), [Time] 
    from #Temp group by [Time] 
    Union All 
     select count(page) as VisitingCount, 
     (datepart(hour,Date)*60+datepart(minute,Date))/30 as [Time] 
     from scr_SecuristLog 
     where Date between '2009-05-04 10:30' and '2009-05-04 12:30' 
     GROUP BY (datepart(hour,Date)*60+datepart(minute,Date))/30--scr_SecuristLog.Date

Meine Anfrage Rückkehr unter Tabelle


0 1
0 2
..(removed repeating)..
0 45
0 46
0 47
825 23
526 21
1064 24
885 22



Das ist mein Traum Tabelle. Ich brauche dies:



0 1
0 2
..(removed repeating)..
0 19
0 20
526 21
885 22
825 23
1064 24
0 25
0 26
..(removed repeating)..
0 46
0 47

Antwort

3

hinzufügen Order By 2 desc zu Ihrem wählen

select Sum(VisitingCount), [Time] 
    from #Temp group by [Time] 
Union All 
    select count(page) as VisitingCount, 
    (datepart(hour,Date)*60+datepart(minute,Date))/30 as [Time] 
    from scr_SecuristLog 
    where Date between '2009-05-04 10:30' and '2009-05-04 12:30' 
    GROUP BY (datepart(hour,Date)*60+datepart(minute,Date))/30--scr 
order by 2 desc 

Beispiel A Siehe in von UNION (Transact-SQL)

+0

statt „GROUP BY (Datumsteil (Stunde, Datum) * 60 + Datumsteil (Minute, Datum))/30 "; wie man GROUP BY [Time] – Penguen

+0

ändert Allerdings nicht ganz genau Sie abfragen. – Penguen

+0

Oh- sollte es aufsteigen? Ich vergesse immer, rate und bearbeite :) –