[英]SQL query with date filter with 14 day interval
I am trying to filter payroll dates
where the result will show me '2011-12-25' plus every 14 days after that up until today
. 我正在尝试过滤
payroll dates
,结果将显示“2011-12-25”以及此后的每14天up until today
。
The query I have is: 我的查询是:
SELECT trxbegdt FROM UPR30300
WHERE TRXBEGDT <= getdate() and trxbegdt >= DATEADD( DAY ,14, '2011-12-25')
group by trxbegdt
order by TRXBEGDT desc
The problem I am having is that the results show dates that are outside of the 14 day intervals which aren't normal payroll transaction begin dates. 我遇到的问题是结果显示超出14天间隔的日期,这不是正常的工资核算交易开始日期。 Would anyone have a solution to comply my conditions?
有人会有解决方案来遵守我的条件吗?
Hows this? 这个怎么样? This uses a recursive CTE to create the list of valid payroll dates, and then joins that to the real table...
这使用递归CTE创建有效工资核算日期列表,然后将其连接到真实表...
WITH PayrollDates AS
(
SELECT CONVERT(datetime,'2011-12-25') AS PayrollDate
UNION ALL
SELECT DATEADD(DAY,14,PayrollDate) AS PayrollDate
FROM PayrollDates
WHERE DATEADD(DAY,14,PayrollDate) <= GETDATE()
)
SELECT
UPR30300.trxbegdt
FROM UPR30300
INNER JOIN PayrollDates
ON UPR30300.trxbegdt = PayrollDates.PayrollDate
SELECT trxbegdt FROM UPR30300 as a where trxbegdt + interval 14 day = b.trxbegdt (SELECT trxbegdt FROM UPR30300 WHERE TRXBEGDT <= sysdate() and trxbegdt >= '2011-12-25' ) As b order by TRXBEGDT desc
SELECT trxbegdt FROM UPR30300 as where trxbegdt + interval 14 day = b.trxbegdt(SELECT trxbegdt FROM UPR30300 WHERE TRXBEGDT <= sysdate()and trxbegdt> ='2011-12-25')按顺序排列TRXBEGDT desc
You may use few variations to get the rolling dates with this constant interval. 您可以使用少量变体来获得此恒定间隔的滚动日期。 Eg
例如
Datediff(day,a.trxbegdt,b.trxbegdt) = 14
dateadd(b.trxbegdt,14,day)= a.trbegdt
PS: it is super painful to format answers in mobile.
PS:在移动设备中格式化答案是非常痛苦的。
plus the code is logic based so run at your end to view results and comment for further updates please.
加上代码是基于逻辑的,所以请在您的最后运行以查看结果并发表评论以获得进一步的更新。
Query to generate dates list between today and your specified date with constant interval of 14 days 查询以生成今天和指定日期之间的日期列表,间隔为14天
set @i:= 0;
SELECT date_format(DATE(ADDDATE('2012-10-05',
INTERVAL @i:=@i+14 DAY)),'%Y-%m-%d')
AS dateP, @i
FROM payroll
HAVING @i < datediff(now(), date '2012-10-05')
;
Above query Output 以上查询输出
DATEP @IntervalDays
2012-10-19 14
2012-11-02 28
2012-11-16 42
2012-11-30 56
2012-12-14 70
Final query that is Supposed to fetch records 是应该获取记录最终查询
set @i:= 0;
SELECT distinct datestamp FROM payroll
WHERE date(datestamp) in (
SELECT DATE(ADDDATE('2012-10-05',
INTERVAL @i:=@i+14 DAY) ) AS dateP
FROM payroll
where @i < DATEDIFF(now(), date '2012-10-05')
)
;
However due to some MYSQL engine behaviour it's not fetching the records, with such ignorane to date comparison. 但是由于一些MYSQL引擎的行为,它没有获取记录,这种无知与日期比较。 So I guess for now, you are better off with above answer.
所以我想现在,你最好回答上面的问题。 I wanted to update this post with the info for the very fact of sharing knowledge.
我想用关于分享知识这一事实的信息来更新这篇文章。
Feel free to comment :) 随意评论:)
As for the findings, in local machines with mysql installed works well for above query by replacing in
clause with inner join
. 至于调查结果, 在安装了mysql的本地机器上,通过用
inner join
替换in
子句, 可以很好地满足上述查询。 However it still doesn't do in SQLFIDDLE. 但是它在SQLFIDDLE中仍然没有。 So the workaround is to delcare variable within nested
select
query. 因此,解决方法是在嵌套
select
查询中使用delcare变量。 Further IN
clause doesn't seem to work well either. 进一步的
IN
子句似乎也不能很好地工作。 Anyway, inner join
outperforms in
clause. 总之,
inner join
性能优于in
的条款。 So final solution is as followed. 所以最终解决方案如下。 :)
:)
Query: 查询:
select p.id, p.datestamp, s.datep from
Payroll as p
inner join
(
SELECT DATE(DATE_ADD('2012-10-05',
INTERVAL @i:=@i+14 DAY) ) AS dateP
FROM Payroll, (SELECT @i:=0) r
where @i < DATEDIFF(now(), date '2012-10-05')
) as s
on p.datestamp = s.dateP
;
Results: 结果:
ID DATESTAMP DATEP
7 October, 19 2012 00:00:00+0000 October, 19 2012 00:00:00+0000
8 November, 02 2012 00:00:00+0000 November, 02 2012 00:00:00+0000
10 November, 16 2012 00:00:00+0000 November, 16 2012 00:00:00+0000
12 November, 30 2012 00:00:00+0000 November, 30 2012 00:00:00+0000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.