2017-05-08 40 views
2

我有一张表来跟踪学生的详细信息,还有另一张表来跟踪学生的表现。如何跟踪sql记录中的最新修改?

+==========================================+ 
| ID  | Department  | Date   | 
+==========================================+ 
| 001  | English   | Jan 3 2017 | 
| 001  | English   | Feb 24 2017 | 
| 001  | Science   | Mar 1 2017 | 
| 001  | Maths   | Mar 2 2017 | 
| 001  | Maths   | Mar 21 2017 | 
| 001  | Maths   | Apr 2 2017 | 
| 001  | English   | Apr 7 2017 | 
| 002  | Maths   | Feb 1 2017 | 
| 002  | Maths   | Apr 7 2017 | 
| 003  | Maths   | Apr 3 2017 | 
| 003  | Maths   | Apr 7 2017 | 
| 004  | Science   | Feb 1 2017 | 
| 004  | Science   | Mar 1 2017 | 
| 004  | Maths   | Apr 7 2017 | 
| 004  | English   | Apr 9 2017 | 
+==========================================+ 

性能表:

+===========================================================================+ 
| ID  | Department   | Best score| Avg score | Date  | 
+===========================================================================+ 
| 001  | English    | 98  | 85   | Jan 30 2017 | 
| 001  | English    | 89  | 80.2  | Apr 14 2017 | 
| 001  | Science    | 75  | 79.8  | May 1 2017 | 
| 001  | Maths    | 88  | 80.2  | Jan 12 2017 | 
| 001  | Maths    | 79  | 75.6  | Feb 21 2017 | 
| 001  | Maths    | 90  | 80.5  | Jan 20 2017 | 
| 001  | English    | 80  | 79.3  | Mar 27 2017 | 
| 002  | Maths    | 90  | 78.4  | Mar 31 2017 | 
| 002  | Maths    | 85  | 80.2  | May 7 2017 | 
| 003  | Maths    | 75  | 79.1  | Apr 30 2017 | 
| 003  | Maths    | 80  | 80.0  | Feb 7 2017 | 
| 004  | Science    | 60  | 70.3  | May 1 2017 | 
| 004  | Science    | 72  | 69.9  | Mar 10 2017 | 
| 004  | Maths    | 70  | 66.8  | Jan 17 2017 | 
| 004  | English    | 65  | 65.0  | Mar 29 2017 | 
+===========================================================================+ 

我想最近的表现,每当一个部门的变化在学生表发生在学生的平均分数。考虑到学生001,学生的部门变化

| 001  | English   | Jan 3 2017 | 
| 001  | Science   | Mar 1 2017 | 
| 001  | Maths   | Apr 2 2017 | 

因为, 2017年1月3日,没有日期小于性能表的日期。

2017年3月1日,最近在性能表中记录日期2017年2月21日

2017年4月2日,最近在性能表中记录日期2017年3月27日

请帮我在正在做。

+0

输出是不是所有你clear.Can重释输出?为什么第一行输出是这样,为什么第二行起? – KumarHarsh

+0

请发布您用于获取该输出的SQL查询吗? –

+0

@KumarHarsh:考虑到学生001部门的变化,他有3条记录。如果我将2017年1月3日的日期与性能表中的日期进行比较,那么在性能表中没有小于此日期的记录。对于2017年3月1日,有4个记录1月30日,1月12日,2月21日和1月20日。但最近的日期是2月21日。 – Poppy

回答

0

取少许疼痛解释清楚,如果我的脚本错误

希望你有解释的输出是正确的。我很抱歉怀疑输出。

最重要的是希望你发布了完全相同的表结构。

由于您的表结构,更多的窗口功能使用比预期,这可能会妨碍您的表现。

关于您的真实需求以及应该如何填充表格结构以及如何填充它们,这些都是非常不同和重要的讨论。

试试这个脚本各种样本数据,让我知道,

declare @StudentDetails table(ID varchar(20) 
,Department varchar(20),Dates Date) 
insert into @StudentDetails VALUES 
('001','English','Jan 3 2017 ') 
,('001','English','Feb 24 2017') 
,('001','Science','Mar 1 2017 ') 
,('001','Maths','Mar 2 2017 ') 
,('001','Maths','Mar 21 2017') 
,('001','Maths','Apr 2 2017 ') 
,('001','English','Apr 7 2017 ') 
,('002','Maths','Feb 1 2017 ') 
,('002','Maths','Apr 7 2017 ') 
,('003','Maths','Apr 3 2017 ') 
,('003','Maths','Apr 7 2017 ') 
,('004','Science','Feb 1 2017 ') 
,('004','Science','Mar 1 2017 ') 
,('004','Maths','Apr 7 2017 ') 
,('004','English','Apr 9 2017 ') 
--select * from @StudentDetails 

declare @Performance table(ID varchar(20) 
,Department varchar(20),Bestscore float,Avgscore float,PDate date) 

insert into @Performance VALUES 
('001','English',98,85 ,'Jan 30 2017') 
,('001','English',89,80.2 ,'Apr 14 2017') 
,('001','Science',75,79.8 ,'May 1 2017 ') 
,('001','Maths',88,80.2 ,'Jan 12 2017') 
,('001','Maths',79,75.6 ,'Feb 21 2017') 
,('001','Maths',90,80.5 ,'Jan 20 2017') 
,('001','English',80,79.3 ,'Mar 27 2017') 
,('002','Maths',90,78.4 ,'Mar 31 2017') 
,('002','Maths',85,80.2 ,'May 7 2017 ') 
,('003','Maths',75,79.1 ,'Apr 30 2017') 
,('003','Maths',80,80.0 ,'Feb 7 2017 ') 
,('004','Science',60,70.3 ,'May 1 2017 ') 
,('004','Science',72,69.9 ,'Mar 10 2017') 
,('004','Maths',70,66.8 ,'Jan 17 2017') 
,('004','English',65,65.0 ,'Mar 29 2017') 
--select * from @Performance 
--declare @SID varchar(20)='001' 

;with CTE as 
(
select * 
,ROW_NUMBER()over(partition by id order by Dates,Department) rn 
from @StudentDetails 
--where [email protected] 
) 
,CTE3 AS(
select c.id, c.Department,c.dates,c.rn,1 rn3 
from cte c 
where rn=1 

union ALL 

select c.id, c.Department,c.dates,c.rn 
,case when c.Department=c3.Department and c.dates>c3.dates 
then cast(c3.rn3 as int) 
else cast(c3.rn3+1 as int) end 
from cte c 
inner join cte3 c3 
on c.id=c3.id 
where c.rn=c3.rn+1 
and c.rn<=7 
) 
,cte4 AS(
select *,0 rn1 from cte3 where rn=1 
union ALL 
select * from 
(
select * ,ROW_NUMBER()over(PARTITION by id,rn3 order by dates desc) rn1 
from cte3 
where rn3>1 
)t4 where t4.rn1=1 
) 

select c.id,c.department,c.dates,fn.Avgscore AVGScroe,fn.pdate RecentPDate 
from cte4 c 
OUTER apply(
select * from 
(select p.*,ROW_NUMBER()over(order by pdate desc)rn2 
from @Performance P 
where c.id=p.id and p.pDate<c.dates)t4 
where rn2=1)fn 
order by c.id 
0

你可以试试这个...

SELECT S.ID,S.DATE,MAX(P.AVG_SCORE),MAX(P.BEST_SCORE) 
FROM STUDENT S, PERFORMANCE P 
WHERE P.DATE < S.DATE AND P.ID = S.ID 
GROUP BY S.ID,S.DATE 

谢谢..