SQL查询,其中日期=今天减去7天 [英] SQL Query Where Date = Today Minus 7 Days

查看:1155
本文介绍了SQL查询,其中日期=今天减去7天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有一个命中SQL表格,称为ExternalHits.我将URL跟踪为URLx,将访问页面的日期跟踪为Datex.我每周运行一次此查询,以获取前一周的总点击数,并且每周我都必须手动更改之间"的日期.有什么方法可以更改查询,以使之间"的日期类似于TODAY AND TODAY-7?我只是不想不必每周手动更改日期.

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 count of total hits from the week before, and every week I have to manually change the "between" dates. Is there some way I can change my query so that the "between" dates are something like TODAY AND TODAY-7? Ijust want to not have to manually change the dates every week.

    SELECT URLX, COUNT(URLx) AS Count
    FROM ExternalHits
    WHERE datex BETWEEN '02/27/2017' AND '03/05/2017'    
    GROUP BY URLx
    ORDER BY Count DESC; 

推荐答案

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; 

这篇关于SQL查询,其中日期=今天减去7天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆