Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 如何对包括当前月份在内的三个月数据求和()_Sql Server 2008_Sum_Common Table Expression - Fatal编程技术网

Sql server 2008 如何对包括当前月份在内的三个月数据求和()

Sql server 2008 如何对包括当前月份在内的三个月数据求和(),sql-server-2008,sum,common-table-expression,Sql Server 2008,Sum,Common Table Expression,我在SQLServer中有一个表,看起来像这样 ProjectId BookedHours FiscalYear FiscalMonth -------------------------------------------------- PRJ1 2040 2015-16 1-Apr-15 PRJ1 1816 2015-16 1-May-15 PRJ1 1760

我在SQLServer中有一个表,看起来像这样

ProjectId   BookedHours    FiscalYear  FiscalMonth
--------------------------------------------------
PRJ1        2040            2015-16     1-Apr-15
PRJ1        1816            2015-16     1-May-15
PRJ1        1760            2015-16     1-Jun-15
PRJ1        1832            2015-16     1-Jul-15
PRJ2        1752            2015-16     1-Sep-15
PRJ2        1529            2015-16     1-Oct-15
PRJ2        1336            2015-16     1-Nov-15
PRJ2        1480            2015-16     1-Dec-15
PRJ2        522             2015-16     1-Jan-16
我需要总结当前+前两个月预订小时的值,即预期结果如下表所示

ProjectId   BookedHours    FiscalYear  FiscalMonth  ExpectedValue
-----------------------------------------------------------------
PRJ1        2040            2015-16     1-Apr-15    2040
PRJ1        1816            2015-16     1-May-15    3856
PRJ1        1760            2015-16     1-Jun-15    5616
PRJ1        1832            2015-16     1-Jul-15    5408
PRJ2        1752            2015-16     1-Sep-15    1752
PRJ2        1529            2015-16     1-Oct-15    3281   
PRJ2        1336            2015-16     1-Nov-15    4617
PRJ2        1480            2015-16     1-Dec-15    4345
PRJ2         522            2015-16     1-Jan-16    3338

这是一种方法

WITH cte AS 
(
    SELECT 
    row_num = ROW_NUMBER() OVER(ORDER BY FiscalMonth),
    * 
    FROM dbo.Project p
)
SELECT CurrentMonth.ProjectID, CurrentMonth.BookedHours, CurrentMonth.FiscalYear, CurrentMonth.FiscalMonth, 
(CurrentMonth.BookedHours + COALESCE(OneMonthBack.BookedHours, 0) + COALESCE(TwoMonths.BookedHours, 0)) AS ExpectedValue
FROM cte CurrentMonth
LEFT JOIN cte OneMonthBack ON OneMonthBack.row_num = CurrentMonth.row_num - 1
LEFT JOIN cte TwoMonths ON TwoMonths.row_num = CurrentMonth.row_num - 2
希望对你有用

    WITH cte AS 
(
    SELECT *,row_num=ROW_NUMBER() OVER( PARTITION BY Projectid ORDER BY Projectid,FiscalYear,FiscalMonth)
    FROM dbo.Project p
)  

SELECT CM.ProjectID, CM.FiscalYear, CM.FiscalMonth, CM.BookedHours, 
(CM.BookedHours + COALESCE(OMB.BookedHours, 0) + COALESCE(TM.BookedHours, 0)) AS ExpectedValue
FROM cte CM
LEFT OUTER JOIN cte OMB WITH(NOLOCK) ON OMB.row_num = CM.row_num - 1 and CM.Projectid=OMB.Projectid
LEFT OUTER JOIN cte TM WITH(NOLOCK) ON TM.row_num = CM.row_num - 2 and CM.Projectid=TM.Projectid
ORDER BY CM.ProjectID, CM.FiscalYear, CM.FiscMonth ASC

上面的查询对我的表非常有效

感谢nscheaffer,它部分有效,您可以从结果表中了解到,对于不同的项目,它再次从第一行开始,您能帮助我吗。。下面的查询给出了我错过的部分结果。您只需添加到每个左连接条件,即可将每个表中的项目设置为相等,但看起来您已经解决了这个问题。干得好。