sql - Query all dates less than the given date (Month and Year) -


i have table: tblperson

there 3 columns in tblperson

id             amort_date        total_amort c000000004     12/30/2015        4584.00 c000000004     01/31/2016        4584.00 c000000004     02/28/2016        4584.00 

the user have provide billing date @bill_date

i want sum total amort of less date given user on month , year basis regardless of date

for example

 @bill_date = '1/16/2016'   result should:   id            sum_total_amort  c000000004    9168.00 

regardless of date want sum amort less january 2016

this query computes date january 2016, not include dates less it:

 declare @bill_date date  set @bill_date='1/20/2016'   declare @month int=month(@bill_date)  declare @year int=year(@bill_date)   select id,sum(total_amort)as'sum_total_amort' webloan.dbo.amort_guide  loan_no='c000000004'  , month(amort_date) = @month    , year(amort_date) = @year   group id 

you use aggregation , inequalities:

select id, sum(total_amort) webloan.dbo.amort_guide loan_no = 'c000000004' ,       year(amort_date) * 12 + month(amort_date) <= @year * 12 + @month group id; 

alternatively, in sql server 2012+, can use eomonth():

select id, sum(total_amort) webloan.dbo.amort_guide loan_no = 'c000000004' ,       amort_date <= eomonth(@bill_date) group id; 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -