如何从表中获取最后一条记录

如何从表中获取最后一条记录

问题描述:

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

我们只知道表名和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)