I have a MySQL database with a table containing a list of dates. I need to find the first and last days in a gap in that table. They are the days off certain employee had in the last 30 days. I am using:
Select DATE, LEAD(DATE,1) over(ORDER BY DATE DESC) AS PREVDATE, datediff(DATE,LEAD(DATE,1) over(ORDER BY DATE DESC)) AS GAP from DAYSWORKED DATE>date_sub(CURRENT_DATE, interval 30 DAY) having GAP>1; If I run the code without the HAVING clause, I get a list of dates and previous dates and the points where GAP>1 are the points where I have the days off, but hwen I include the clause HAVING to filter those points it doesn't work.
What am I doing wrong?
Thanks for your help !!!
it doesn't work... what is the exact error message you are getting? Your query is missing aWHEREclause, but maybe this is just a copying error.show create table DAYSWORKEDand some insert statements for sample data and desired and actual results for that sample data that demonstrate the problemGAPcolumn contains window function which is not allowed in HAVING clause, even implicitly. Because it affects the output rowset and hence it affects window function (self) result which is obtained between HAVING and ORDER BY.