Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
Maciej Los 4-Mar-13 5:18am    
emp_id is not primary key and unique column?
[no name] 4-Mar-13 5:21am    
Maan_k ... Do you have unique values in emp_id column ?
If the emp_id is not unique, then why you are keeping it as a column in this table.
No use of it, right ?


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

 
Share this answer
 
v6
Comments
[no name] 4-Mar-13 5:20am    
Your answer is good, but in question he told that he don't have any unique valued column.
Hi,

Try to Use WITH and ROW_NUMBER() FUNCTION as follows

SQL
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)

Regards,
GVPrabu
 
Share this answer
 
v4
SQL
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)
 
Share this answer
 
Try this
SQL
select  *
from    (
        SELECT  row_number() over(order by BogusRowOrder) as RowNum,
                count(*)     over()            as RowCnt,
                *
        from    (
                SELECT  1 as BogusRowOrder,
                        *
                FROM    employee
                ) as a
        ) as b
where   RowNum = RowCnt
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900