查询查看最近6个月的记录

本文关键字:记录 6个 最近 查询 | 更新日期: 2023-09-27 17:49:24

select sum(Invoice_Amount) as Invoice_amount,
       COUNT(*) as Invoice_Count 
from Payment_Exception pe
  inner join Invoice_Details ind on ind.Customer_ReferenceId = pe.Id 
where pe.Customer_Code = '1001012'
  and ind.Status='Submit' 

查询查看最近6个月的记录

select sum(Invoice_Amount) as Invoice_amount,COUNT(*) as Invoice_Count 
from Payment_Exception pe inner join Invoice_Details ind on ind.Customer_ReferenceId=pe.Id 
where pe.Customer_Code='1001012' and ind.Status='Submit'  
and ind.invoice_date>=DATEADD(m,-6,getdate())
group by Invoice_Amount

还需要添加group by condition

select sum(Invoice_Amount)
as Invoice_amount,COUNT(*) as Invoice_Count
from Payment_Exception pe 
inner join Invoice_Details ind on ind.Customer_ReferenceId=pe.Id 
where pe.Customer_Code='1001012' and ind.Status='Submit' and  
ind.Insert_Date > dateadd(m, -6, getdate() - datepart(d, getdate()) + 1)