简体   繁体   中英

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 .

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. 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...

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

\n

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. 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

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. 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 :)


  • Updating after above mentioned variable scope is resolved.

As for the findings, in local machines with mysql installed works well for above query by replacing in clause with inner join . However it still doesn't do in SQLFIDDLE. So the workaround is to delcare variable within nested select query. Further IN clause doesn't seem to work well either. Anyway, inner join outperforms in clause. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM