Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

consecutive rows sql server 2008

$
0
0

Hi,

I need to find idno that have policies both with collector 1 followed by collector 2. I need to find the first occurence of each.

If a certain IDNO has 2 occurrences of 1,2,1,2 i need to return only the first 1,2. If idno has only 1 or 2 then it's not relevant.

I would appreciate the help please. Thanks :-)

CREATE TABLE #Payments(
	[ID] [int] IDENTITY NOT NULL,
	[Policy] [int] NOT NULL,
	[IdNo] [int] NOT NULL,
	[DateCreated] [datetime] NULL)--2009-12-30 00:00:00.000
	INSERT INTO #Payments VALUES(5667088,30043256,'2012-08-07') 
	INSERT INTO #Payments VALUES (5634342,30043256,'2012-08-10') 
	INSERT INTO #Payments VALUES (7854544,30043256,'2012-09-07') 
	INSERT INTO #Payments VALUES (4345477,30043256,'2012-10-08') 
	INSERT INTO #Payments VALUES (651754,27569106,'2012-09-07') 
	INSERT INTO #Payments VALUES (651723,27569106,'2012-09-08') 
	INSERT INTO #Payments VALUES (181754,0234199,'2009-12-27') 
	INSERT INTO #Payments VALUES (655349,0234199,'2009-12-30') 
	INSERT INTO #Payments VALUES (181074,2905645,'2012-09-07') 
	INSERT INTO #Payments VALUES (123978,2905645,'2012-09-08') 
CREATE TABLE #POLICIES(
	[Policy] [int] NOT NULL,
	[IdNo] [int] NOT NULL,
	[Collector] [int] NOT NULL)
	INSERT INTO #POLICIES VALUES (5667088,30043256,1) 
	INSERT INTO #POLICIES VALUES (5634342,30043256,2) 
	INSERT INTO #POLICIES VALUES (7854544,30043256,1) 
	INSERT INTO #POLICIES VALUES (4345477,30043256,2) 
	INSERT INTO #POLICIES VALUES (651754,27569106,1) 
	INSERT INTO #POLICIES VALUES (651723,27569106,2) 
	INSERT INTO #POLICIES VALUES (181754,0234199,1) 
	INSERT INTO #POLICIES VALUES (655349,0234199,1) 
	INSERT INTO #POLICIES VALUES (181074,2905645,2) 
	INSERT INTO #POLICIES VALUES (123978,2905645,2) 

output:

IDNO

Policy

COLLECTOR

DateCreated

30043256

5667088

1

2012-08-07

30043256

5634342

2

2012-08-10

27569106

651754

1

2012-09-07

27569106

651723

2

2012-09-08


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>