2015-09-10 36 views
0

所以,我有以下2列我如何根據值-1對列進行求和?

,sum(case when Owner_Faculty_Full_Desc = 'Business and Law Faculty' then 1 else 0 end) [Business Current Year] 
,sum(case when Owner_Faculty_Full_Desc = 'Business and Law Faculty' AND Apps.Reporting_Year = (Apps.Reporting_Year - 1) then 1 else 0 end) [Business Previous Year] 

讓我們說Apps.ReportingYear2011,怎麼我得到的第二列總結在2010所有值?我認爲我的發言會起作用,但似乎並非如此。

這裏是整個查詢

WITH Equiv_day AS (
    SELECT Date_Key, Sem1_Equivalent_Day 
    FROM [AUTDataWarehouse].[dbo].[Dim_Date] 
    WHERE Full_Date = CONVERT(DATE, GETDATE()) 
), 
Date_list AS ( 
    SELECT dt.Date_Key, dt.Calendar_Year 
    FROM [AUTDataWarehouse].[dbo].[Dim_Date] AS dt 
    INNER JOIN Equiv_day AS ed 
     ON (ed.Sem1_Equivalent_Day - 1) = dt.Sem1_Equivalent_Day 
     AND dt.Calendar_Year IN (YEAR(GETDATE()), YEAR(GETDATE())-1, YEAR(GETDATE()) - 2, YEAR(GETDATE()) - 3, YEAR(GETDATE()) - 4, YEAR(GETDATE()) - 5) 
), 

PYTD_Outcome_by_Application AS (
    SELECT 
     se.Reporting_Year, 
     f.Student_Demographics_Key, 
     f.Application_Code, 
     Outcome_Row = Max(f.Application_Row_Num) 
    FROM AUTDataWarehouse.dbo.Fact_Admission as f 
     INNER JOIN AUTDataWarehouse.dbo.Dim_Intake as it 
      ON it.Intake_Key = f.Intake_Key 
      AND it.Prog_Intake_Type_Desc = 'Intake' 
     INNER JOIN AUTDataWarehouse.dbo.Dim_Semester as se 
      ON se.Semester_Key = f.Semester_Key 
     INNER JOIN AUTDataWarehouse.dbo.Dim_Campus as cp 
      ON cp.Campus_Key = f.Campus_Key 
     INNER JOIN Date_list as dates 
      ON dates.Calendar_Year = se.Reporting_Year 
    WHERE f.Last_Change_Date_Key < dates.Date_Key 
    GROUP BY 
     se.Reporting_Year, 
     f.Student_Demographics_Key, 
     f.Application_Code 
), 
Applications_Count AS (
    SELECT 

     p.Owner_Faculty_Full_Desc, 
     p.Programme_Full_Desc, 
     et.Enrolment_Type_Key, 
     se.Reporting_Year, 
     sd.Last_Secondary_School_Name [LastSchoolName], 
     pr.Programme_Key, 
     Campus = CASE WHEN cp.Campus_Name IN ('South','South Dist') THEN 'South' ELSE 'City/North Shore' END, 
     se.Semester_Desc, 
     f.Student_Demographics_Key, 
     Applicants = COUNT(DISTINCT f.Student_Demographics_Key) 
     FROM AUTDataWarehouse.dbo.Fact_Admission as f 
     INNER JOIN AUTDataWarehouse.dbo.Dim_Student_Demographics as sd ON sd.Student_Demographics_Key = f.Student_Demographics_Key 
     INNER JOIN AUTDataWarehouse.dbo.Dim_Programme as P on P.Programme_Key = f.Programme_Key 
     INNER JOIN AUTDataWarehouse.dbo.Dim_Intake as it 
      ON it.Intake_Key = f.Intake_Key 
      AND it.Prog_Intake_Type_Desc = 'Intake' 
     INNER JOIN AUTDataWarehouse.dbo.Dim_Enrolment_Type as et 
      ON et.Enrolment_Type_Key = f.Enrolment_Type_Key 
     INNER JOIN AUTDataWarehouse.dbo.Dim_Programme as pr 
      ON pr.Programme_Key = f.Programme_Key 
     INNER JOIN AUTDataWarehouse.dbo.Dim_Campus as cp 
      ON cp.Campus_Key = f.Campus_Key 
     INNER JOIN AUTDataWarehouse.dbo.Dim_Semester as se 
      ON se.Semester_Key = f.Semester_Key 
     INNER JOIN Date_list as dates 
      ON dates.Calendar_Year = se.Reporting_Year 
     INNER JOIN PYTD_Outcome_by_Application AS pytd 
      ON pytd.Application_Code = f.Application_Code 
      AND pytd.Student_Demographics_Key = f.Student_Demographics_Key 
      AND pytd.Outcome_Row = f.Application_Row_Num 
      and pytd.Reporting_Year = se.Reporting_Year 
      AND pytd.Application_Code IS NOT NULL 
    GROUP BY 

     sd.Last_Secondary_School_Name, 
     p.Owner_Faculty_Full_Desc, 
     p.Programme_Full_Desc, 
     et.Enrolment_Type_Key, 
     se.Reporting_Year, 
     pr.Programme_Key, 
     CASE WHEN cp.Campus_Name IN ('South','South Dist') THEN 'South' ELSE 'City/North Shore' END, 
     se.Semester_Desc, 
     f.Student_Demographics_Key 
) 

SELECT 

CAST(EP.Educational_Provider_Key as varchar(10)) as 'Education Key' 
,[LastSchoolName] 
,Owner_Faculty_Full_Desc 
,Programme_Full_Desc 
,Apps.Reporting_Year 
,cast(cast(Apps.Reporting_Year as varchar(255)) + '0101' as datetime) as [YearTime] 
,sum(case when Owner_Faculty_Full_Desc = 'Business and Law Faculty' then 1 else 0 end) [Business and Law Current Year] 
,sum(case when Owner_Faculty_Full_Desc = 'Business and Law Faculty' AND (Apps.Reporting_Year = Apps.Reporting_Year - 1) then 1 else 0 end) [Business and Law Previous Year] 
,sum(case when Owner_Faculty_Full_Desc = 'Culture and Society Faculty' then 1 else 0 end) [Culture and Society Current Year] 
,sum(case when Owner_Faculty_Full_Desc = 'Culture and Society Faculty' AND Apps.Reporting_Year = 2014 then 1 else 0 end) [Culture and Society Previous Year] 
,sum(case when Owner_Faculty_Full_Desc = 'Design & Creative Technologies Faculty'then 1 else 0 end) [Design & Creative TechnologiesCurrent Year] 
,sum(case when Owner_Faculty_Full_Desc = 'Design & Creative Technologies Faculty' AND Apps.Reporting_Year = (Apps.Reporting_Year - 1) then 1 else 0 end) [Design & Creative Technologies Previous Year] 
,sum(case when Owner_Faculty_Full_Desc = 'Health & Environmental Sciences Faculty'then 1 else 0 end) [Health & Environmental Sciences Current Year] 
,sum(case when Owner_Faculty_Full_Desc = 'Health & Environmental Sciences Faculty' AND Apps.Reporting_Year = (Apps.Reporting_Year - 1) then 1 else 0 end) [Health & Environmental Sciences Previous Year] 
,sum(case when Owner_Faculty_Full_Desc = 'Te Ara Poutama' then 1 else 0 end) [Te Ara Poutama Current Year] 
,sum(case when Owner_Faculty_Full_Desc = 'Te Ara Poutama' AND Apps.Reporting_Year = (Apps.Reporting_Year - 1) then 1 else 0 end) [Te Ara Poutama Previous Year] 
,sum(apps.Applicants) [Total Applicants] 


FROM Applications_Count as Apps 

inner join AUTDataWarehouse.dbo.Dim_Educational_Provider as EP on EP.Provider_Name = [LastSchoolName] 

GROUP BY Apps.Reporting_Year, [LastSchoolName], EP.Educational_Provider_Key,Owner_Faculty_Full_Desc 
, Programme_Full_Desc 

ORDER BY Apps.Reporting_Year, [Total Applicants] DESC 

所以基本上每個程序我想總的申請人ReportingYear然後總申請人爲Reporting Year - 1

由於一列!

+3

的問題是,對於一個給定的行'Apps.Reporting_Year'可以」 t等於'Apps.Reporting_Year - 1'。對於當前年份,您可以使用'YEAR(Apps.Reporting_Year)= YEAR(GETDATE())'和'YEAR(Apps.Reporting_Year)= YEAR(GETDATE()) - 1'。假設它是'DATE'字段。 –

+0

@HARTCO,這是一個答案。所以請張貼它。 – Rahul

+0

@HARTCO它不是'DATE'字段,而是一個varchar,它擁有2010-2015年的數據,所以對於每一行我都需要'Apps.Reporting_Year'的數據,然後在它旁邊有一個來自'Apps的數據列。 Reporting_Year - 1' –

回答

0

可能最容易分別計算年份,然後自行加入以獲得上一年的價值。這是你可以使用基本的邏輯,雖然很明顯,你需要使它更加複雜,當你使其適應上述長查詢:

;with CTE as (select ReportingYear 
    , sum(case when [condition] then 1 else 0 end) as ValueCurrYear 
    from MyTables 
    group by ReportingYear) 

Select a.*, b.ValueCurrYear as ValuePreviousYear 
from CTE a 
left join CTE b 
on a.ReportingYear = b.ReportingYear + 1 
相關問題