Solved

Record extraction

Posted on 2016-11-10
3
26 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 28

Expert Comment

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
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…

739 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