Har denna kod för att plocka ut antalet träffar de senaste 20 dagarna. Det känns som jag borde kunna göra en bättre SQL fråga istället för att använda denna loop. Behöver dock lite hjälp, tack!
strDag = Left(Now,10)
For i = 0 To 19
arrDagar(19-i) = day(strDag)
strSQL = "SELECT count(IPnummer) FROM tblStatistikBesokare WHERE year(Tidpunkt)= " & year(strDag) & " AND month(Tidpunkt)= " & month(strDag) & " AND day(Tidpunkt)= " & day(strDag)
Set RecSet = Connect.Execute(strSQL)
arrVarden(19-i) = RecSet(0)
strDag = DateAdd("d", -1, strDag)
Next
jag använder MSSQL så jag tog bort #-tecknerna. ändrade även "group by 1" till "group by tidpunkt", annars fick jag detta felmedelande:
"GROUP BY expressions must refer to column names that appear in the select list."
när jag skriver date() så får jag:
'date' is not a recognized function name.
och när jag skriver date, altså utan parenteser:
Invalid column name 'date'.
select convert(varchar(10),tidpunkt,101) as tidpunkt, count(ipnummer) as antal
from tblStatistikBesokare
where tidpunkt between dateadd(d,-20,getdate()) and getdate()
group by convert(varchar(10),tidpunkt,101)
Det svaret som jag får ut från denna SQL sats vill jag ha in i två arrayer, en med "tidpunkt" (egentligen bara dag-delen av datumet) och en med "antal". Dessa skickas till en funktion som ritar upp det hela. Det blir dock ett problem om en dag saknar besökare, då blir det ju ingen rad i SQL satsen. Kan jag fixa det på SQL nivå, eller måste jag ta det när jag skapar arraysen? om det senare, hur kan man på ett smart sätt göra det?
select convert(varchar(10),dateadd(d,-c,getdate()) as tidpunkt, count(ipnummer) as antal
from (select 0 as c union all select 1 union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6 union all select 7 union all
select 8 union all select 9 union all select 10 union all select 11 union all
select 12 union all select 13 union all select 14 union all select 15 union all
select 16 union all select 17 union all select 18 union all select 19) dt
left join tblStatistikBesokare
on convert(varchar(10),dateadd(d,-c,getdate()),101) = convert(varchar(10),tidpunkt)
where tidpunkt between dateadd(d,-20,getdate()) and getdate()
group by convert(varchar(10),dateadd(d,-c,getdate())
Om man gör sådant ofta kan man med fördel skapa en permanent tabell med integervärden. Annars så går det lika bra med en skriptbaserad lösning.
Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'as'.
Server: Msg 170, Level 15, State 1, Line 6
Line 6: Incorrect syntax near 'dt'.
vad kan det bero på?
ser förstår jag inte riktigt hur jag skulle kunna göra en permanent tabell här, förutom att göra en jättetabell med alla dagar de närmast kommande åren och sätta antalet till noll, men det låter ju inte helt bra.
:) (Räkna parenteser och kontrollera argumenten till convert.)
select convert(varchar(10),dateadd(d,-c,getdate()),101) as tidpunkt,
count(ipnummer) as antal
from (select 0 as c union all select 1 union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6 union all select 7 union all
select 8 union all select 9 union all select 10 union all select 11 union all
select 12 union all select 13 union all select 14 union all select 15 union all
select 16 union all select 17 union all select 18 union all select 19) dt
left join tblStatistikBesokare
on convert(varchar(10),dateadd(d,-c,getdate()),101) = convert(varchar(10),tidpunkt,101)
where tidpunkt between dateadd(d,-20,getdate()) and getdate()
group by convert(varchar(10),dateadd(d,-c,getdate()),101)
Du behöver inte lagra datum, det räcker med integervärden.
Får fortfarande problem med de dagar som saknar poster i databasen. Tex den 2:a och den 3:e oktober vill jag gärna få ut i listan nedan fast med antal 0.
select convert(varchar(10),dateadd(d,-c,getdate()),101) as tidpunkt,
count(ipnummer) as antal
from (select 0 as c union all select 1 union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6 union all select 7 union all
select 8 union all select 9 union all select 10 union all select 11 union all
select 12 union all select 13 union all select 14 union all select 15 union all
select 16 union all select 17 union all select 18 union all select 19) dt
left join tblStatistikBesokare
on convert(varchar(10),dateadd(d,-c,getdate()),101) = convert(varchar(10),tidpunkt,101)
and tidpunkt between dateadd(d,-20,getdate()) and getdate()
group by convert(varchar(10),dateadd(d,-c,getdate()),101)
Då det är en left outer join så må villkoren på den högra tabellen läggas i on och inte i where