查询最近6个月的记录?

时间:2015-05-15 07:11:56

标签: c# sql sql-server

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' 

2 个答案:

答案 0 :(得分:1)

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

您还需要按条件添加分组。

答案 1 :(得分:0)

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)