SQL选择具有特定状态的特定学生的最新记录

SQL选择具有特定状态的特定学生的最新记录,sql,sql-server,Sql,Sql Server,请帮助我选择状态为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

请帮助我选择状态为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的每个学生的最新记录?

您需要使用
分组依据

select student_id, max(date)
from table_name
where status=1
group by student_id;

您需要使用
分组依据

select student_id, max(date)
from table_name
where status=1
group by student_id;

您需要使用
分组依据

select student_id, max(date)
from table_name
where status=1
group by student_id;

您需要使用
分组依据

select student_id, max(date)
from table_name
where status=1
group by student_id;

在这个查询中,很明显,
status=1
在我的select中添加了它,如下所示:

Select Student_ID ,max(Date) Date, 1 status From table1 
where status = 1 Group by Student_ID;

在这个查询中,很明显,
status=1
在我的select中添加了它,如下所示:

Select Student_ID ,max(Date) Date, 1 status From table1 
where status = 1 Group by Student_ID;

在这个查询中,很明显,
status=1
在我的select中添加了它,如下所示:

Select Student_ID ,max(Date) Date, 1 status From table1 
where status = 1 Group by Student_ID;

在这个查询中,很明显,
status=1
在我的select中添加了它,如下所示:

Select Student_ID ,max(Date) Date, 1 status From table1 
where status = 1 Group by Student_ID;
试试这个

SELECT LAST(column_name) FROM table_name;
;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
试试这个

SELECT LAST(column_name) FROM table_name;
;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
试试这个

SELECT LAST(column_name) FROM table_name;
;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
试试这个

SELECT LAST(column_name) FROM table_name;
;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
试试这个

SELECT LAST(column_name) FROM table_name;
;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
试试这个

SELECT LAST(column_name) FROM table_name;
;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
试试这个

SELECT LAST(column_name) FROM table_name;
;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
试试这个

SELECT LAST(column_name) FROM table_name;
;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

如果您使用的是sql server,请尝试以下操作

Select top(1) * from StudentTable where status =1 order by student_id desc

如果您使用的是sql server,请尝试以下操作

Select top(1) * from StudentTable where status =1 order by student_id desc

如果您使用的是sql server,请尝试以下操作

Select top(1) * from StudentTable where status =1 order by student_id desc

如果您使用的是sql server,请尝试以下操作

Select top(1) * from StudentTable where status =1 order by student_id desc
在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
在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
在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
在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

您可以使用
ROW\u 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

您可以使用
ROW\u 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

您可以使用
ROW\u 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

您可以使用
ROW\u 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