请帮我选择状态= 1的每位学生的最新记录。我有一个名为stdData
和列
Student ID FDate Status
12 2014-03-12 1
12 2014-03-15 1
13 2014-02-03 1
13 2014-02-04 0
13 2014-02-05 1
如何选择状态= 1的每位学生的最新记录?
答案 0 :(得分:1)
您需要使用group by
select student_id, max(date)
from table_name
where status=1
group by student_id;
答案 1 :(得分:1)
在此查询中很明显status = 1
我将其添加到我的选择中,如下所示:
Select Student_ID ,max(Date) Date, 1 status From table1
where status = 1 Group by Student_ID;
答案 2 :(得分:1)
如果您使用的是sql server,请尝试使用
Select top(1) * from StudentTable where status =1 order by student_id desc
答案 3 :(得分:0)
试试这个!
SELECT LAST(column_name) FROM table_name;
答案 4 :(得分:0)
试试这个!
;with cte as
(
select *,rn=row_number() over(partition by Student_Id order by Date desc) from #t
)
select * from cte where rn=1 and status=1
答案 5 :(得分:0)
在Sqlserver
中Create table Student(
ID int identity(1,1) primary key not null,
S_Date datetime,
Status bit
)
insert into Student values ('2014-03-11',1)
insert into Student values ('2014-03-12',0)
insert into Student values ('2014-03-13',1)
insert into Student values ('2014-03-14',0)
insert into Student values ('2014-03-15',1)
select top 1 * from Student where Status=1 Order By S_Date Desc
答案 6 :(得分:0)
您可以使用ROW_NUMBER
功能来实现此目的:
SELECT Student_ID, Date , Status
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY Student_ID ORDER BY Date DESC) AS rn, Student_ID, Date , Status
FROM your_table
WHERE Status = 1
) tab
WHERE tab.rn = 1