我使用多个MAX语句来获取"最后一个活动"对于学生。学生每年注册的入学记录。除了一个字段外,我得到了所有正确的结果.. 学生注册的最后一所学校的学校名称。 最后一所学校可以通过获得保留最后一个录入日期的记录中的学校名称......但我似乎无法找到解决方案,或者无法将代码放在正确的位置......或者某些东西。我试过的不同方式是在底部缩进评论。 DECLARE @grade char(2); 设置@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
尝试使用上次注册学校名称的解决方案
/* 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
答案 0 :(得分:0)
使用cross apply()
获取Student_EnrollmentCube
的最新行,了解最新的入学日期和学校名称
declare @grade char(2);
set @grade = '00';
select
sd.Last_Name
, sd.First_Name
, sd.Middle_Name
, Name_Suffix = sd.Name_Appendage
, Date_Of_Birth = convert(char(10), convert(date,sd.dob), 101)
, Student_Number = sd.Student_Number
, Academic_Year = max(se.Academic_Year)
, Latest_Withdrawal_Date = convert(char(10), max(convert(date,se.Withdrawal_date)), 101)
, Latest_Graduation_Date = convert(char(10), max(convert(date,se.Graduation_Date)), 101)
, State_Id = max(sd.doe_Number)
, x.Last_Entry_Date
, x.School_Name
, Graduation_Year = max(se.Graduation_Year)
, Grade_Level = case when max(try_convert(int,se.Grade_Level)) > 12 then @grade else se.Grade_Level end)
from Student_DemographicsCube sd
inner join Student_EnrollmentCube se
on sd.Student_id = se.Student_id
cross apply (
select top 1
i.School_Name
, Last_Entry_Date = i.Entry_Date
from Student_EnrollmentCube i
where i.Student_id = si.Student_id
order by convert(date,i.Entry_Date) desc
) x
where sd.student_Number = 700010068
and se.Entry_Date > convert(date,'20011231')
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
, x.Last_Entry_Date
, x.School_Name
order by
sd.Student_Number asc
, x.Last_Entry_Date asc
备注/参考:
varchar
without (length) - Aaron Bertrand datetime
和smalldatetime
来说是:YYYYMMDD
和YYYY-MM-DDThh:mm:ss[.nnn]