Solved

Record extraction

Posted on 2016-11-10
3
12 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 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
Output needed for the above data will be ?
0
 
LVL 17

Accepted Solution

by:
Pawan Kumar Khowal earned 500 total points
Comment Utility
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
Comment Utility
Thanks Pawan
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

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…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

11 Experts available now in Live!

Get 1:1 Help Now