如何从表中获取最后一条记录 [英] how to fetch the last record from table

查看:105
本文介绍了如何从表中获取最后一条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果没有主键和唯一列,如何从表中获取最后一条记录

我们只知道表名和colunm名称。





示例

表名=员工

列名= emp_id,emp_name,sal。

how to fetch the last record from table when there is no primary key and unique column
only we know the table name and colunm name.


example
table name=employee
column name=emp_id,emp_name,sal.

推荐答案




select TOP 1  * from  [employee] order by  empID desc




您好,



尝试使用WITH和ROW_NUMBER()函数如下



Hi,

Try to Use WITH and ROW_NUMBER() FUNCTION as follows

WITH employeeDtls(Row_Number,emp_id,emp_name,sal)
AS
(   SELECT ROW_NUMBER() OVER(ORDER BY emp_id) 'Row_Number',emp_id, emp_name,sal 
    FROM employee 
) 
SELECT emp_id, emp_name,sal  FROM employee 
WHERE Row_Number = (SELECT COUNT(emp_id) FROM employee)



问候,

GVPrabu


Regards,
GVPrabu


with cte 
as 
(select empid,empname,sal,
row_number() over(order by empid) as rows
)
select empid,empname,sal
from cte
group by empid,empname,sal
having rows= max(rows)


这篇关于如何从表中获取最后一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆