最近一个月的总和值

时间:2011-10-18 13:54:30

标签: sql-server

我正在使用2个表,t_funds和t_psg2_nav。

在t_funds表中,相关列为fundnum和br_cd 在t_psg2_nav中,相关列是calcdate,return_active_daily和fundnum

基本上我需要一个查询,它总结了最近一个月每个基金的return_active_daily。我想我需要使用一个JOIN子句,但现在对我来说有点勉强。这就是我所拥有的...我正在使用的t_funds表仅用于在fundnum和br_cd(一个名字)之间切换......这对我来说更容易。

SELECT funds.br_cd AS 'Fund Ticker'
    , Month(nav.calcdate) AS 'Month'
    , SUM(nav.return_active_daily) AS 'MTD Tracking'


FROM dbo.t_psg2_nav AS nav
    , t_funds AS funds

INNER JOIN (SELECT fundnum
                    ,month(max(calcdate)) AS calcdate
            FROM    dbo.t_psg2_nav
            GROUP BY fundnum
           )  AS nav1 
ON nav.fundnum = nav1.fundnum
AND nav.calcdate = nav1.calcdate

WHERE funds.fundnum = nav.fundnum
AND funds.fund_type LIKE 'ETF'

1 个答案:

答案 0 :(得分:0)

将其附加到SQL的末尾:

HAVING Month=Month(NOW())

或等同地;

HAVING Month=Month(SYSDATE())

(编辑)的

仅显示来自f.ex.的记录三月(第3个月):

HAVING Month=3