I'm using multiple MAX statements to get the "last activity" for students. Students have an enrollment record for each year they enroll. I get all the correct results with the exception of one field..the school name of the last school the student was enrolled at. The last school can be found by getting the school name from the record that holds the last entry date...but I can't seem to find a solution, or haven't put the code in the correct place..or something. The different ways I've tried are indented comments at the bottom. DECLARE @grade char(2); Set @grade='00'
Select
SD.[Last_Name],
SD.[First_Name],
SD.[Middle_Name],
SD.[Name_Appendage] as [Name_Suffix],
convert(varchar,cast(SD.[DOB] as date),101) as [Date_Of_Birth],
SD.[Student_Number] as [Student_Number],
Max (SE.[Academic_Year]) as [Academic_Year],
Max (CONVERT(VARCHAR, SE.[Withdrawal_Date], 101)) as
[Last_Withdrawal_Date],
Max (CONVERT(VARCHAR, SE.[Graduation_Date], 101)) as
[Graduation_Date],
Max (SD.[DOE_Number]) as [State_Id],
Max (CONVERT(VARCHAR, SE.[Entry_Date], 101)) as [Last_Entry_Date],
Max (SE.[Graduation_Year]) as [Graduation_Year],
Max (CASE when SE.[Grade_Level] > '12' then @grade
else SE.[Grade_Level]
End) as [Grade_Level]
From
Student_DemographicsCube as SD WITH (NOLOCK),
Student_EnrollmentCube as SE WITH (NOLOCK)
Where
SD.[Student_ID] = SE.[Student_ID] And
SE.[Entry_Date] > cast('12/31/2001' as date) And
SE.[School_Domain] in
(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,26,27,28,29,30,31)
Group By
SD.[Student_Number],
SD.[First_Name],
SD.[Middle_Name],
SD.[Last_Name],
SD.[Name_Appendage],
SD.[DOB]
Order By
SD.[Student_Number] asc,
Max (SE.[Entry_Date]) asc
Attempted Solutions to last enrolled at school name
/* Select TOP (1) (SE.[School_Name]) from */
/* Student_EnrollmentCube order by (SE.[Entry_Date) desc ) */
/* (Select SE.[School_Name] from Student_EnrollmentCube as */
/* ST with (NOLOCK) where @holddate = SE.[Entry_Date]) */
/*SE.[School_Name] as [School_Name] */
/* Max (CASE when Max (SE.[Entry_Date]) = SE.[Entry_Date] */
/* then SE.[School_Name] */
/* else @school */
/* End) as [School_Name] */
/* Select TOP (1) SE.[School_Name] from */
/* Student_EnrollmentCube Group by SE.[Student_Number] */
/* order by SE.[Entry_Date] desc */
/*ROW_NUMBER() OVER (PARTITION BY SE. */
/* [Student_NUmber],SE.[Academic_Year] ORDER BY SD.*/
/* [Student_Number],SE.[Academic_Year]) as seqnum */
/* FROM SE.[Academic_Year] t */
/* ) t */
/* WHERE seqnum = 1; */
/*6/2/17 Code____________________________________________/*
DECLARE @grade char(2);
Set @grade='00'
Select
SD.[Last_Name],
SD.[First_Name],
SD.[Middle_Name],
SD.[Name_Appendage] as [Name_Suffix],
convert(varchar,cast(SD.[DOB] as date),101) as [Date_Of_Birth],
SD.[Student_Number] as [Student_Number],
Max (SE.[Academic_Year]) as [Academic_Year],
convert(varchar,cast(w.[Last_Withdrawal_date] as date),101) as
[Latest_Withdrawal_Date] ,
convert(varchar,cast(g.[Last_Graduation_Date] as date),101) as
[Latest_Graduation_Date] ,
Max (SD.[DOE_Number]) as [State_Id],
Max (CONVERT(VARCHAR, e.[Entry_Date], 101)) as [Last_Entry_Date],
Max (SE.[Graduation_Year]) as [Graduation_Year],
Max (CASE when SE.[Grade_Level] > '12' then @grade
else SE.[Grade_Level]
End) as [Grade_Level],
e.[School_Name]
From
Student_DemographicsCube SD WITH (NOLOCK),
Student_EnrollmentCube SE WITH (NOLOCK)
cross apply (select top 1 e.[School_Name], e.[Entry_Date] from
Student_EnrollmentCube e
where e.[Student_id] = se.[Student_id] and se.[entry_date] >
cast('12/31/2001' as date) order by e.[Student_ID], e.[Entry_Date]
desc) i
cross apply (select top 1 w.[Withdrawal_Date] as [Last_Withdrawal_Date]
from Student_EnrollmentCube w
where w.[Student_id] = se.[Student_id] and se.[entry_date] >
cast('12/31/2001' as date) order by w.[Student_id], w.[Withdrawal_Date]
desc) w
cross apply (select top 1 g.[Graduation_Date] as [Last_Graduation_Date]
from Student_EnrollmentCube g
where g.[Student_id] = se.[Student_id] and se.[entry_date] >
cast('12/31/2001' as date) order by g.[Student_id], g.[Graduation_Date]
desc) g
Where
SD.[Student_ID] = SE.[Student_ID] And sd.[student_Number] = 700010068 and
SE.[Entry_Date] > cast('12/31/2001' as date) And
SE.[School_Domain] in
(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,13,24,25,26,27,28,29,30,31)
Group By
SD.[Student_Number],
SD.[First_Name],
SD.[Middle_Name],
SD.[Last_Name],
SD.[Name_Appendage],
SD.[DOB],
e.[school_name],
w.[Last_Withdrawal_Date],
g.[Last_Graduation_Date]
Order By
SD.[Student_Number] asc,
Max (SE.[Entry_Date]) asc