所以我有这个代码,它可以获取这些列/数据及其所属的表:
StudentName -- ST
stunum -- ST
ssn -- ST
Campus -- CA
SchoolStatus -- SS
Program -- AE
AYStart -- FS
AYEnd -- FS
AYStatus -- PS
StaffName -- SF
fastudentayid -- AU
DateAdded -- AU
这是我的代码
select rtrim(st.lastname) +', '+rtrim(st.FirstName) as StudentName,
st.StuNum,
st.SSN as SSN,
ca.Descrip as Campus,
ss.Descrip as SchoolStatus,
ae.adProgramDescrip as Program,
convert(varchar(10),fs.StartDate,101) as AYStart,
convert(varchar(10),fs.EndDate,101) as AYEnd,
ps.Descrip as AYStatus,
rtrim(sf.lastname) +', '+rtrim(sf.FirstName) as StaffName,
fs.faStudentAyID as faStudentAyID,
convert(varchar(10), MAX(af.DateAdded),101) as DateAdded
from stuTbl (nolock) as ST
join CpsTbl (nolock) as CA
on ca.CpsID = st.CpsID
join scStatTbl (nolock) as SS
on ss.ScStatTblID = st.ScStatTblID
join AdEnTbl (nolock) as AE
on ae.stuTblID = st.stuTblID
join faStAy (nolock) as FS
on fs.AdEnTblID = ae.AdEnTblID
join FaPStat (nolock) as PS
on ps.FaPStatID = fs.FaPStatID
join (select RecordID, ColumnName, NewVal, UserID, DateAdded, MAX(DateAdded) as MDA
from syA_FaPStatTbl
where ColumnName = 'Package Status'
and (NewVal = '38'
or NewVal = '40'
or NewVal = '43'
or NewVal = '67'
or NewVal = '68')
and DateAdded between getDate()-90 and getDate()
group by RecordID, ColumnName, NewVal, UserID, DateAdded) as AF
on af.RecordID = fs.faStAyID
join StaffTbl (nolock) as SF
on af.UserID = sf.StaffTblID
where (ps.Descrip = 'Submitted'
or ps.Descrip = 'Resubmitted'
or ps.Descrip = 'Pell Submitted'
or ps.Descrip = 'Aid Submitted'
or ps.Descrip = 'Aid Resubmitted')
and af.DateAdded between getDate()-90 and getDate()
Group by st.lastname, st.FirstName,
st.StuNum,
st.SSN,
ca.Descrip,
ss.Descrip,
ae.adProgramDescrip,
fs.StartDate,
fs.EndDate,
ps.Descrip,
sf.lastname, sf.FirstName,
fs.faStudentAyID
我得到了我需要的数据,因为我没有复制条目。我的问题是,如果有人改变客户端软件的状态,只要更改在今天的90天之内,它就会将状态切换为“已提交”的两个实例都拉出来。
这是一个示例数据 - 请注意Crytal Ball的记录?有人更新了状态,并于5月11日和6/5/2018再次提交。无论状态如何更新,我都需要最新的记录,无论过去90天发生了多少次,在这种情况下是6/5/2018。
Jones, Mary || 124926 || xxx-xx-xxxx || Seattle || Active || MCA Prog || 05/28/2018 || 12/23/2018 || Submitted || Doe, John || 1763799 || 06/06/2018
Doe, Dawn || 126954 || xxx-xx-xxxx || Online || Ready to Start || MBC Prog || 05/28/2018 || 12/23/2018 || Resubmitted || Jones, Bob, || 1760731 || 06/06/2018
Ball, Crystal || 12399 || xxx-xx-xxxx || Chattanooga || Active || MCA Dipl || 07/02/2018 || 02/10/2019 || Submitted || Jones, Jenny || 1734032 || 05/11/2018
Ball, Crystal || 12399 || xxx-xx-xxxx || Chattanooga || Active || MCA Dipl || 07/02/2018 || 02/10/2019 || Resubmitted || Tavares, John || 1734032 || 06/05/2018
Barnes, Matt || 11817 || xxx-xx-xxxx || Online || Drop || 4 yr BSAH Mgt || 04/23/2018 || 11/18/2018 || Submitted || Doe, Luis || 1759782 || 04/27/2018
编辑: - 我已经尝试将Top 1放在代码的JOIN(Select)部分,它不会让我拉任何记录。
答案 0 :(得分:0)
您需要做的只是使用 row_number()来获取正确的数据,如下所示
select
StudentName,StuNum,SSN,Campus,
SchoolStatus,Program,AYStart,AYEnd,AYStatus,
StaffName,faStudentAyID,DateAdded
from
(select *,
row_number() over( partition by fs.faStudentAyID order by DateAdded) as rn
from
(
select rtrim(st.lastname) +', '+rtrim(st.FirstName) as StudentName,
st.StuNum,
st.SSN as SSN,
ca.Descrip as Campus,
ss.Descrip as SchoolStatus,
ae.adProgramDescrip as Program,
convert(varchar(10),fs.StartDate,101) as AYStart,
convert(varchar(10),fs.EndDate,101) as AYEnd,
ps.Descrip as AYStatus,
rtrim(sf.lastname) +', '+rtrim(sf.FirstName) as StaffName,
fs.faStudentAyID as faStudentAyID,
convert(varchar(10), MAX(af.DateAdded),101) as DateAdded
from stuTbl (nolock) as ST
join CpsTbl (nolock) as CA
on ca.CpsID = st.CpsID
join scStatTbl (nolock) as SS
on ss.ScStatTblID = st.ScStatTblID
join AdEnTbl (nolock) as AE
on ae.stuTblID = st.stuTblID
join faStAy (nolock) as FS
on fs.AdEnTblID = ae.AdEnTblID
join FaPStat (nolock) as PS
on ps.FaPStatID = fs.FaPStatID
join (select RecordID, ColumnName, NewVal, UserID, DateAdded, MAX(DateAdded) as MDA
from syA_FaPStatTbl
where ColumnName = 'Package Status'
and (NewVal = '38'
or NewVal = '40'
or NewVal = '43'
or NewVal = '67'
or NewVal = '68')
and DateAdded between getDate()-90 and getDate()
group by RecordID, ColumnName, NewVal, UserID, DateAdded) as AF
on af.RecordID = fs.faStAyID
join StaffTbl (nolock) as SF
on af.UserID = sf.StaffTblID
where (ps.Descrip = 'Submitted'
or ps.Descrip = 'Resubmitted'
or ps.Descrip = 'Pell Submitted'
or ps.Descrip = 'Aid Submitted'
or ps.Descrip = 'Aid Resubmitted')
and af.DateAdded between getDate()-90 and getDate()
Group by st.lastname, st.FirstName,
st.StuNum,
st.SSN,
ca.Descrip,
ss.Descrip,
ae.adProgramDescrip,
fs.StartDate,
fs.EndDate,
ps.Descrip,
sf.lastname, sf.FirstName,
fs.faStudentAyID
) t
)t
where rn=1