MySQL使用lag()函数时忽略null值
我想计算全年发生的付款月份差异。例如,如果我在2月份有100款付款,& 120在三月份,差异将为20。我已经做了一个使用lag()的查询,但是我面临的唯一问题是查询显示为无效值。由于2月之前没有付款,所以月botonth将显示为null,我想跳过该行。 以下是示例数据集
,这是我正在使用的查询,
SELECT date_format(payment_date,'%M') 'Month', COUNT(*) - LAG(COUNT(*))
OVER (ORDER BY FIELD(date_format(payment_date,'%M'),
'January','February','March','April','May','June','July','August','September','October','November','December'))
AS 'MonthByMonthChange'
from main
GROUP BY date_format(payment_date,'%M')
ORDER BY FIELD(date_format(payment_date,'%M'),'January','February','March','April','May','June','July','August','September','October','November','December');
还附加了要获得的输出。
I want to calculate the MonthByMonth difference of payments happening throughout the year. For example, if I had 100 payments in February & 120 in the month of March, the difference would be 20. I have already made a query that uses LAG(), but the only problem I'm facing is that the query is showing NULL Value. Since there are no payments before February, the MonthByMonth would show NULL, I WANT TO SKIP THAT ROW.
Below is the sample Dataset
And this is the query I'm using
SELECT date_format(payment_date,'%M') 'Month', COUNT(*) - LAG(COUNT(*))
OVER (ORDER BY FIELD(date_format(payment_date,'%M'),
'January','February','March','April','May','June','July','August','September','October','November','December'))
AS 'MonthByMonthChange'
from main
GROUP BY date_format(payment_date,'%M')
ORDER BY FIELD(date_format(payment_date,'%M'),'January','February','March','April','May','June','July','August','September','October','November','December');
Also attaching the output I'm getting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
子查询,然后在按月更改字段上添加检查
null
记录。Subquery and then add a check on the month by month change field to filter off
NULL
records.