Solved

Record extraction

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server 2012 r2 - Sum totals 2 29
Linked Server Issue with SQL2012 3 29
PROPERCASE SCRIPT IN SQL 3 16
how to restore or keep sql2000  backups useful... 2 18
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

820 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