使用t-sql的年度数据报告

时间:2015-01-07 13:25:58

标签: sql-server tsql

我有简单的项目数据

Project     Year    Sum
512-17      2011    3147625,79
512-43      2011    652935,22
512-43      2012    23982,68
512-52      2012    584541,35
512-52      2013    3353388,73
512-52      2014    3935164,39

如何将结果数据从2010年初始化为当前年度

Project     Year    Sum
512-17      2010    0 -- if not exist in current data init with year and zero Cost
512-17      2011    3147625,79
512-17      2012    0
512-17      2013    0
512-17      2014    0
512-17      2015    0
512-43      2010    0
512-43      2011    652935,22
512-43      2012    23982,68
512-43      2013    0
512-43      2014    0
512-43      2015    0
512-52      2010    0
512-52      2011    0
512-52      2012    584541,35
512-52      2013    3353388,73
512-52      2014    3935164,39
512-52      2015    0

4 个答案:

答案 0 :(得分:1)

这应该做:

;WITH Years AS
(
    SELECT 2010+number [Year]
    FROM master.dbo.spt_values
    WHERE type = 'P'
    AND 2010+number <= YEAR(GETDATE())
)
SELECT  P.Project,
        Y.[Year],
        ISNULL(T.[Sum],0) [Sum]
FROM Years Y
CROSS JOIN (SELECT DISTINCT Project
            FROM dbo.YourTable) P
LEFT JOIN dbo.YourTable T
    ON Y.[Year] = T.[Year]
    AND P.Project = T.Project 

你需要从你想要的每年列表开始(我使用数字表做了这个,但是你应该在某个地方有一个包含这些信息的表)

答案 1 :(得分:0)

刚刚在另一个帖子上发布了类似的代码:

DECLARE @StartYear INT = 2010
DECLARE @EndYear INT = 2014

;WITH MyCte ([Year])
AS
(
    SELECT  [Year] = @StartYear
    UNION ALL
    SELECT  [Year] + 1 FROM MyCte WHERE [Year] < @EndYear
)
,ProjectList ([Project], [Year], [Sum])
AS
(
    SELECT '512-17', 2011, '3147625,79' UNION
    SELECT '512-43', 2011, '652935,22' UNION
    SELECT '512-43', 2012, '23982,68' UNION
    SELECT '512-52', 2012, '584541,35' UNION
    SELECT '512-52', 2013, '3353388,73' UNION
    SELECT '512-52', 2014, '3935164,39'
)
SELECT  DISTINCT P.[Project], M.[Year], [Sum] = 0
FROM    ProjectList P
CROSS APPLY (SELECT [Year] FROM MyCTE WHERE [Year] NOT IN (SELECT [Year] FROM ProjectList WHERE [Project] = P.[Project])) M

答案 2 :(得分:0)

select 
  project
 ,y.year
 ,Sum
from (select 2010 as year
      union all
      select 2011
      union all
      select 2012
      union all
      select 2013
      union all
      select 2014
      union all
      select 2015) y
left join pd 
  on y.year = pd.year
order by Project
        ,y.year

答案 3 :(得分:0)

使用Recursive CTE

 with
 CTE
 as
 (
 select N=1,0 as d
 union all
 select N+1,2009+N as d from CTE where N<7
 )select distinct Tab.Project,d ,ISNULL(Tab1.[Sum],0) from CTE 
 CROSS JOIN   (SELECT DISTINCT Project
            FROM dbo.MyTable)   Tab
    LEFT JOIN MyTable Tab1
 on CTE.d=Tab1.[Year] 
 and Tab.Project=Tab1.Project
 where d!=0
 ORDER BY Tab.Project,d