?
Solved

Record extraction

Posted on 2016-11-10
3
Medium Priority
?
34 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 37

Expert Comment

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

Accepted Solution

by:
Pawan Kumar earned 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

840 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