如何在sql server中获取从当前日期时间到最后7天的最后7天数据
您好,我正在使用pentaho将表A数据从sql server加载到mysql 在加载数据时,我只需要从SQLServerA表到mysql中获取最近7天的数据 在sql server createddate中,列数据类型类似于datetime和 在mysql中,列上的数据类型是timestamp 这里我使用了下面的查询,但我只得到了5天的数据如何在sql server中获取从当前日期时间到最后7天的最后7天数据,sql,sql-server,sql-server-2008-r2,pentaho,pentaho-cde,Sql,Sql Server,Sql Server 2008 R2,Pentaho,Pentaho Cde,您好,我正在使用pentaho将表A数据从sql server加载到mysql 在加载数据时,我只需要从SQLServerA表到mysql中获取最近7天的数据 在sql server createddate中,列数据类型类似于datetime和 在mysql中,列上的数据类型是timestamp 这里我使用了下面的查询,但我只得到了5天的数据 请帮我解决这个问题 select id, NewsHeadline as news_headline, NewsText as news_
请帮我解决这个问题
select id,
NewsHeadline as news_headline,
NewsText as news_text,
state,
CreatedDate as created_on
from News
WHERE CreatedDate BETWEEN GETDATE()-7 AND GETDATE()
order by createddate DESC
尝试以下方法:
SELECT id, NewsHeadline as news_headline, NewsText as news_text, state CreatedDate as created_on
FROM News
WHERE CreatedDate >= DATEADD(day,-7, GETDATE())
我不认为你有过去七天里每一天的数据。没有数据的日子显然不会出现 尝试此操作并验证您是否拥有过去7天内每天的数据
SELECT DISTINCT CreatedDate
FROM News
WHERE CreatedDate >= DATEADD(day,-7, GETDATE())
ORDER BY CreatedDate
编辑-从您的评论中复制
我有12月19-1行数据,18-2行,17-3行,16-3行 第15-3排、第12-2排、第11-4排、第9-1排、第8-1排 您没有所有天的数据。这是你的问题,而不是问题。如果您今天执行查询(22日),您将只获得19日、18日、17日、16日和15日的数据。您没有20、21和22的数据 编辑-要获取过去7天的数据,如果数据可用,您可以尝试
select id,
NewsHeadline as news_headline,
NewsText as news_text,
state,
CreatedDate as created_on
from News
WHERE CreatedDate IN (SELECT DISTINCT TOP 7 CreatedDate from News
order by createddate DESC)
如果您想使用Pentaho DI执行此操作,可以使用“修改的JavaScript”步骤并编写以下函数:
dateAdd(d1, "d", -7); // d1 is the current date and "d" is the date identifier
检查下图:[假设当前日期为:2014年12月22日]
希望有帮助:)希望这会有帮助
select id,
NewsHeadline as news_headline,
NewsText as news_text,
state,
CreatedDate as created_on
from News
WHERE CreatedDate >= cast(dateadd(day, -7, GETDATE()) as date)
and CreatedDate < cast(GETDATE()+1 as date) order by CreatedDate desc
选择id,
新闻标题作为新闻标题,
新闻文本作为新闻文本,
国家,,
CreatedDate作为在上创建的
来自新闻
其中CreatedDate>=cast(dateadd(day,-7,GETDATE())作为日期)
和CreatedDate<按CreatedDate desc转换(GETDATE()+1作为日期)顺序
DATEADD和GETDATE函数可能无法在MySQL数据库中工作。因此,如果您使用的是MySQL数据库,那么下面的命令可能会对您有所帮助
select id, NewsHeadline as news_headline,
NewsText as news_text,
state, CreatedDate as created_on
from News
WHERE CreatedDate>= DATE_ADD(CURDATE(), INTERVAL -3 DAY);
我希望它能对你有所帮助这对我来说很有效强>
SELECT * FROM `users` where `created_at` BETWEEN CURDATE()-7 AND CURDATE()
您可以在where子句中使用DATEADD函数,如
select ...... where Createdate >= DATEADD(day,-7,GETDATE())
要获取过去3天而非当前日期的数据,请执行以下操作:
date(timestamp) >= curdate() - 3
AND date(timestamp) < curdate()
date(timestamp)>=curdate()-3
和日期(时间戳)
例如:
SELECT *
FROM user_login
WHERE age > 18
AND date(timestamp) >= curdate() - 3
AND date(timestamp) < curdate()
LIMIT 10
选择*
从用户登录
年龄>18岁
和日期(时间戳)>=curdate()-3
和日期(时间戳)
您确定有7天的数据吗?是的,我确定我有7天的数据过去7天中的每一天都必须有数据我有12月19-1行数据,18-2行,17-3行,16-3行,15-3行,12-2行,11-4行,9-1行,第8-1行也请确保您在表news中有过去7天的数据我有2010年至2014年12月19日的过去数据我有12月19-1行数据、18-2行、17-3行、16-3行、15-3行、12-2行、11-4行、9-1行、8-1行在这种情况下,如果我想得到12、11、9,请留下14和13行数据,第8个数据如何帮助我完成上述查询它正在提供最后7个记录而不是最后7天记录以其他方式获取最近7天,包括当前日期:[查询的其余部分]。。。其中CreatedDate介于(选择CAST(DATEADD(day,-6,GETDATE())作为日期))和(选择CAST(GETDATE()作为日期))
SELECT *
FROM user_login
WHERE age > 18
AND date(timestamp) >= curdate() - 3
AND date(timestamp) < curdate()
LIMIT 10