SQL Query Where Date = Today Minus 7 Days

前端 未结 7 1132
花落未央
花落未央 2020-12-18 21:47

I have a SQL table of hits to my website called ExternalHits. I track the URL as URLx and the date the page was accessed as Datex. I run this query every week to get the cou

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-18 22:24

    declare @lastweek datetime
    declare @now datetime
    set @now = getdate()
    set @lastweek = dateadd(day,-7,@now)
    
    SELECT URLX, COUNT(URLx) AS Count
    FROM ExternalHits
    WHERE datex BETWEEN @lastweek AND @now
    GROUP BY URLx
    ORDER BY Count DESC; 
    

提交回复
热议问题