而在T-SQL中循环

时间:2015-04-20 16:36:52

标签: sql sql-server tsql sql-server-2008-r2

是否有办法根据特定条件循环内部联接。我返回的数据如下:

 Term          EnrollmentTotal  Retained
 Fall 2011            X             X
 Fall 2012            X             X
 Fall 2013            X             X

实际代码:

DECLARE 
 @Version int = 2 
,@TermSourceKey varchar(40)= '1109,1119,1129'
,@NextTerm varchar(40) = '1119,1129,1139'

/* ******** TERMSOURCKEY PARAMETER ********** */
if OBJECT_ID('Tempdb..#TermSourceKeyTable','U') is not null
    drop table #TermSourceKeyTable

CREATE TABLE #TermSourceKeyTable
    (Terms varchar(5) )

INSERT INTO #TermSourceKeyTable
SELECT value from HEA.ParseList(@TermSourceKey,',')

if OBJECT_ID('Tempdb..#NextTermParam','U') is not null
    drop table #NextTermParam

CREATE TABLE #NextTermParam
    (ID INT Identity(1,1)
    ,val varchar(20)
    ,NTerm varchar(5) )

INSERT INTO #NextTermParam
SELECT 
 0
,value from HEA.ParseList(@NextTerm,',')

/* ******** NEXT TERM PARAMETER ********** */

if OBJECT_ID('Tempdb..#EnrollmentTypes','U') is not null
    drop table #EnrollmentTypes

    Create Table #EnrollmentTypes
    (
    EmployeeID Varchar(12)
    ,PlanOwnerKey int
    ,Term varchar(25)
    ,NumberEnrolled int
    --,BaseID varchar(12)
    ,RetainedInDept int)

Insert Into #EnrollmentTypes  
SELECT 
 Distinct fst.EmployeeID
--,fst.EmployeeID
,dpo.PlanOwnerKey
,dt.Description 
,0
,0 --Count(et.NumberEnrolled) as 'Total Enrolled'
,0 --RetainedInDept

FROM Final.FactStudentTerm fst

    JOIN CustomFinal.FactStudentPlanSimple fsps
        on fst.EmployeeID = fsps.EmployeeID
            and fst.AcademicCareer = fsps.AcademicCareer
            and fst.TermSourceKey = fsps.TermSourceKey
            and fst.VersionKey = fsps.VersionKey
            and fst.Institution = fsps.Institution
            and fsps.AcademicCareer = 'UGRD'

    Join CustomFinal.FactPlanOwnership fpo
        on fsps.AcademicPlan = fpo.AcademicPlan
            and fsps.PlanKey = fpo.PlanKey
            and fsps.Institution = fpo.Institution

    JOIN CustomFinal.DimPlanOwner dpo
        on fpo.PlanOwnerKey = dpo.PlanOwnerKey
            and dpo.CollegeOrgCode = @CollegeOrgCode
            and dpo.PlanOwnerKey = @PlanOwnerKey       /* Biological Sciences */

    JOIN CustomFinal.DimProgramStatusForPlan dpsp
        on dpsp.ProgramStatusForPlanKey = fsps.ProgramStatusForPlanKey /* This table returns: only students active in their program*/
            and dpsp.ProgramStatusSourceKey = 'AC'

    JOIN Final.DimProgram dprog
        on fst.ProgramKey = dprog.ProgramKey                /* Check to see if these should be included: Associate Degree Program (ASSOC), Undergraduate Second Degree (UG2D), Undergraduate Degree Seeking (UGDEG) */
            and dprog.SourceKey in ('UGDEG','UG2D')  

    JOIN Final.DimPlan dp
        on fsps.PlanKey = dp.PlanKey
            and dp.PlanType in ('MAJ','CRT')

    JOIN Final.DimTerm dt
        on fst.TermKey = dt.TermKey

    JOIN Final.DimStudent ds
        on fst.EmployeeID =ds.SourceKey

    Join #TermSourceKeyTable tskt
        on tskt.Terms = fst.TermSourceKey

WHERE 1 = 1
AND fst.VersionKey = @Version
AND fst.EnrolledIndicatorKey in ('11','12') 

GROUP BY dpo.PlanOwnerKey , fst.EmployeeID, dt.Description
/* END OF INSERT INTO SQL: Returns NumberEnrolled */

/* ************* ENROLLED, BUT SWITCHED DEPARTMENT ************ */
UPDATE #EnrollmentTypes
SET EnrollButSwitchedDept = EnrollButSwitchedDept.EnrollButSwitchedDept
FROM #EnrollmentTypes et
outer apply (SELECT CASE WHEN et.PlanOwnerKey != dpo1.PlanOwnerKey then 1 else 0 end as EnrollButSwitchedDept
             FROM Final.FactStudentTerm fst1

    JOIN CustomFinal.FactStudentPlanSimple fsps
        on fst1.EmployeeID = fsps.EmployeeID
            and fst1.AcademicCareer = fsps.AcademicCareer
            and fst1.TermSourceKey = fsps.TermSourceKey
            and fst1.VersionKey = fsps.VersionKey
            and fst1.Institution = fsps.Institution
            and fsps.AcademicCareer = 'UGRD'

    JOIN CustomFinal.FactPlanOwnership fpo
        on fsps.AcademicPlan = fpo.AcademicPlan
            and fsps.PlanKey = fpo.PlanKey
            and fsps.Institution = fpo.Institution

    JOIN CustomFinal.DimPlanOwner dpo1
        on fpo.PlanOwnerKey = dpo1.PlanOwnerKey

    JOIN CustomFinal.DimProgramStatusForPlan dpsp
        on dpsp.ProgramStatusForPlanKey = fsps.ProgramStatusForPlanKey
            and dpsp.ProgramStatusSourceKey = 'AC'

    JOIN Final.DimProgram dprog
        on fst1.ProgramKey = dprog.ProgramKey
            and dprog.SourceKey in ('UGDEG','UG2D')

    JOIN Final.DimPlan dp
        on fsps.PlanCount = dp.PlanKey
            and dp.PlanType in ('MAJ','CRT')

    JOIN Final.DimTerm dt
        on fst1.TermKey = dt.TermKey

    JOIN #NextTermParam NTP
        on fst1.TermSourceKey = NTP.NTerm  /*comparing 2010 to 11, 12, & 13*/
            and NTP.ID = 1 --+1

    --JOIN Final.DimTerm dtEnrolled
    --  on dtEnrolled.NextYearTermSourceKey = fst1.TermSourceKey
    --      and dtEnrolled.NextYearTermSourceKey = dt.NextYearTermSourceKey

    --JOIN #TermSourceKeyTable tsk
    --  on tsk.Terms = fst1.termsourcekey

WHERE 1 = 1
AND fst1.EmployeeID = et.EmployeeID
AND fst1.VersionKey = @Version
--AND fst1.TermSourceKey = @NextTerm
AND fst1.EnrolledIndicatorKey in ('11','12')
)
EnrollButSwitchedDept


/* Main Query */
SELECT
 et.Term
,COUNT(et.EmployeeID) as 'Enrollment Total'
,SUM(et.EnrollButSwitchedDept) as 'EnrollButSwitched'
FROM #EnrollmentTypes et
WHERE 1 = 1
--AND et.EnrollButSwitchedDept is not null
GROUP BY 
 et.Term
ORDER BY et.Term 

我遇到的主要问题是2011年秋季学期被用来计算所有年份的保留期。因此,我想我需要循环连接,以便为正在计算的当前项拉取@NextTerm。有什么建议吗?

1 个答案:

答案 0 :(得分:2)

你永远不想在SQL中做循环。 SQL使用集合上的集合和操作。因此,例如,您想要使用名为COUNT()的函数计算给定术语的行数。您的查询可能如下所示:

Select .... subselect.cnt as Next_Term_Enrollment
from ....
join (
  select term, count(*) as cnt
  from tmptable
  group by term
) as subselect on (subselect.term-1) = term

既然你提供了关于你的数据模型的非常细节,我可以更具体地说明这一点...但我希望这能让你朝着正确的方向前进。