Solved

Record extraction

Posted on 2016-11-10
3
20 Views
Last Modified: 2016-11-10
I have data like this  in the table. I want to extract records which do not have record type cancel in that record id’s last record and if a record has cancelled and renewed again it should only show the records from renewal row to end.

Please let me know how can this be accomplished?



A111      New      1
A111      Cancel      2
A111      Renew      3
A111      Change      4
A112      New      1
A112      Change      2
A112      Change      3
A113      New      1
A113      Cancel      2
A114      New      1
A114      Cancel      2
A114      Renew      3
A114      Change      4
A114      Cancel      5
0
Comment
Question by:Sriv
  • 2
3 Comments
 
LVL 24

Expert Comment

by:Pawan Kumar
ID: 41882547
Output needed for the above data will be ?
0
 
LVL 24

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41882585
Try..

CREATE TABLE testCancel
(
	 Id VARCHAR(10)
	,Type VARCHAR(10)
)
GO

INSERT INTO testCancel VALUES
('A111',      'New'    ),
 ('A111',      'Cancel'),
 ('A111',      'Renew'),
 ('A111',      'Change'),
 ('A112',      'New'),
 ('A112',      'Change'),
 ('A112',      'Change'),
 ('A113',      'New'),
 ('A113',      'Cancel'),
 ('A114',      'New'),
 ('A114',      'Cancel'),
 ('A114',      'Renew'),
 ('A114',      'Change'),
 ('A114',      'Cancel')
GO

Open in new window



Query

SELECT Id,Type FROM
(
	SELECT *, MAX(CASE Type WHEN 'Cancel' THEN rnk ELSE 0 END) OVER (PARTITION BY Id) CancelId , MAX(rnk) OVER (PARTITION BY Id) maxi FROM 
	(
		SELECT * ,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Id) rnk FROM testCancel
	)n
)m WHERE CancelId <> maxi AND (rnk > CancelId)

Open in new window



Output
----------


Id             Type
A111      Renew
A111      Change
A112      New
A112      Change
A112      Change


Hope it helps !!
0
 

Author Closing Comment

by:Sriv
ID: 41882669
Thanks Pawan
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
CONVERT date time to a different time zone. 2 44
sql join/ assign small # first 10 83
SQL Update Query - What's wrong with this. 18 20
Minus first query 1 36
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now