Click here to Skip to main content
15,882,163 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
;WITH emp_hierarachy as(
SELECT EmployeeId as emp,EmployeeId, Manager,Firstname + ' ' + surname as name,Position  FROM tblEmployee
UNION ALL
SELECT rs.emp,t.EmployeeId,t.Manager, t.Firstname + ' ' + surname as name,t.Position FROM tblEmployee t 
INNER JOIN emp_hierarachy rs ON t.EmployeeId = rs.Manager
)

select row_number() OVER (ORDER BY(select 1)) id,rs.name,rs.Position from emp_hierarachy rs
where rs.emp = 179 

--and rs.EmployeeId != rs.emp 

order by id desc

What I have tried:

limit with order by but it is not working


give me soluiton
Posted
Updated 21-May-19 1:21am

1 solution

Have you experimented with TOP? What were the results?
SQL
SELECT TOP 3 Row_Number()...
Updated. The last 3 rows can be achieved if you reverse the ORDERing you have in place. Here is the full query based upon this:
SQL
;WITH emp_hierarachy AS(
	SELECT	emp = EmployeeId, EmployeeId, Manager, Firstname + ' ' + surname as [name], Position
	FROM		tblEmployee
		UNION ALL
	SELECT	rs.emp, t.EmployeeId, t.Manager, t.Firstname + ' ' + surname as [name], t.Position
	FROM		tblEmployee     t
	INNER JOIN emp_hierarachy rs ON t.EmployeeId = rs.Manager
)

	SELECT	TOP 3 id = Row_Number() OVER (ORDER BY (SELECT 1))
		,	rs.name
		,	rs.Position
	FROM		emp_hierarachy     rs
	WHERE	rs.emp = 179 
--	AND	rs.EmployeeId	!= rs.emp 
	ORDER BY ID -- DESC
Notes/Questions/Questions
Please note that the name field is wrapped in square brackets. Name is one of them words that is certain contexts is special.I generally would avoid using them.

In your commented out AND clause, you are using != for inequality. While this is valid in SQL Server, the ANSI standard is to use <>; so the portability of this code may be limited.

The CTE seems to be gathering more data than it needs to; only the EmployeeID, [name] and position columns are called upon, and the UNION ALL is going to add in duplicate records.

I would be interested in seeing sample data and the results you expect to come out; as I do not know the schema of these tables but I think this could be greatly reduced codewise
 
Share this answer
 
v2
Comments
Member 14405442 21-May-19 7:29am    
select last 3 row n
not top 3 raw....
phil.o 21-May-19 7:38am    
Technically, to select the last 3 rows, you reverse the sort order and take the top 3.
Edit:
SELECT TOP 3 Row_Number() ... ORDER BY id ASC
Herman<T>.Instance 21-May-19 8:08am    
Yep, this is the solution! Post it as a solution!
phil.o 21-May-19 8:19am    
I'm not sure a second solution would add any value. Issue may even be resolved by now without a closure from OP.
But thanks for the virtual 5, I appreciate it :)
MadMyche 21-May-19 9:48am    
I will update the answer, was pressed for time when I posted originally

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