![](/img/trans.png)
[英]SQL Query to Select * From Table where date is between 7 days ago and now
[英]SQL - where MIN date is exactly 7 days ago
我只需要选择 MIN 日期为 7 天前的行。 我有发票的客户,发票有到期日。 我可以为每个客户选择这些发票的最小到期日。 我现在卡住了,因为我不能只选择最旧发票过期 7 天的客户。
这就是我所拥有的:
select
customerID,
MIN(dueDate) as min_due_date
from invoices
where (invoiceStatus NOT IN ('PaidPosted','Cancelled'))
and (entity = 'HQ')
group by (customerID)
我尝试添加:
and min_due_date = dateadd(day, -7, SYSDATE())
这不起作用。 我究竟做错了什么?
谢谢。
使用having
子句过滤最小日期:
select customerID, min(dueDate) as min_due_date
from invoices
where invoiceStatus not in ('PaidPosted', 'Cancelled') and
entity = 'HQ'
group by customerID
having min(duedate) = current_date - interval '7 day';
请注意,日期函数是高度特定于数据库的。 以上是标准 SQL,但确切的语法取决于您使用的数据库。
谢谢你,戈登,你让我走上了正轨! 这就是最终成功的秘诀:
select customerID, min(dueDate) as min_due_date
from invoices
where invoiceStatus not in ('PaidPosted', 'Cancelled') and
entity = 'HQ'
having MIN(dueDate) = trunc(sysdate) -7
group by customerID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.