SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 how to select longest and active row
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

satish15385
Starting Member

17 Posts

Posted - 08/07/2012 :  12:07:51  Show Profile  Reply with Quote
emplid -- payplan -- Termid -- Contractperiod -- datepayend
24 -- 22 -- 201205 -- UA -- 2012-06-07
24 -- 22 -- 201205 -- UB -- 2012-07-05

I want to select the contractperiod based on following conditions:
if it has more than one appointment (case like here) and contractperiod in ('UA','UB') then
the longest hire date from the date 2012-05-07 has to be selected
if datepayend is same then max(contractperiod) has to be selected

let me know your inputs

Edited by - satish15385 on 08/07/2012 12:18:50

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 08/07/2012 :  12:22:50  Show Profile  Reply with Quote

SELECT *
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY emplid,payplan ORDER BY datepayend DESC,ContractPeriod DESC) AS Seq,
COUNT(CASE WHEN contractperiod IN ('UA','UB') THEN 1 ELSE NULL END) OVER (PARTITION BY emplid,payplan) AS Occ,
*
FROM Table
)t
WHERE Seq=1 
OR Occ<=1 


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000