Link to home
Start Free TrialLog in
Avatar of maximus1974
maximus1974

asked on

How to return the last row using ROW_COUNT

How can I have the following statement below return the last or highest row for rn?

SELECT wo_number, first_name, last_name
    FROM (SELECT wo_number,
                 first_name,
                 last_name,
                 ROW_NUMBER() OVER(PARTITION BY wo_number ORDER BY first_name, last_name) rn,
                 COUNT(*) OVER (PARTITION BY wo_number) cnt
            FROM ba_view_wo_labor
           WHERE skill NOT LIKE '%INSPECTOR')
   WHERE cnt > 1 AND rn = 1
ORDER BY wo_number

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

try this:
select top 1 l.wo_number, l.first_name, l.last_name
from dbo.ba_view_we_labour l
where
	l.skill not like '%inspector'
order by
	l.wo_number desc
;

Open in new window

Regards
  David


PS Not going to perform very well on larger datasets
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial