开发者

How to write select query for this?

开发者 https://www.devze.com 2023-01-20 00:07 出处:网络
I have column COMPONENT_AMOUNT(money) ,MONTH_FOR(int), YEAR_FOR(int) and COMPONENT_TYPE(int). I want to find the sum of amount according to condition. I have written my query like this but it solve my

I have column COMPONENT_AMOUNT(money) ,MONTH_FOR(int), YEAR_FOR(int) and COMPONENT_TYPE(int). I want to find the sum of amount according to condition. I have written my query like this but it solve my purpose.

My purpose is that if one select Nov-2010 then it will sum the amount up to this selected month including all the month of 2009.

My query is given bel开发者_开发百科ow ,please modify it:

SELECT SUM(Convert(Numeric(7,2), Round([COMPONENT_AMOUNT],2,1))) 
FROM [TEAM_FUNDS_DETAILS] 
WHERE YEAR_FOR <= 1 AND MONTH_FOR <= 1 AND [COMPONENT_TYPE] = 1  


If You need only one result per query I think that this is code

declare @year int 
declare @month int

set @year = 2010 --desire year
set @month = 8   --desire month

select sum(COMPONENT_AMOUNT)
from [TEAM_FUNDS_DETAILS] 
WHERE
    [COMPONENT_TYPE] = 1
    and  (
           YEAR_FOR < @year
        or (year_for=@year and month_for<=@month)
          )
0

精彩评论

暂无评论...
验证码 换一张
取 消