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

Return values not seen in previous records

$
0
0

I'd like to be able to return Code values below that have not occurred previously (ordered by ID).  I have a feeling it's going to involve a ROW_NUMBER() and CTE, but I can't quite put my finger on it.  See the example below.

DROP TABLE #codes
CREATE TABLE #codes (ID INT NOT NULL,
				 Code VARCHAR(20) NOT NULL)

ALTER TABLE #codes
ADD CONSTRAINT pk_codes PRIMARY KEY (ID, Code)

INSERT INTO #codes (ID,
				Code)
VALUES
(1, 'A'),
(1, 'B'),
(1, 'C'),
(1, 'D'),
(1, 'F'),
(2, 'A'),
(2, 'B'),
(2, 'C'),
(2, 'D'),
(2, 'E'),-- not seen in #1 - return this record
(3, 'B'),
(3, 'C'),
(3, 'D'),
(3, 'E'),
(3, 'F'),-- not seen in #2 but was seen in #1 so no need to return this record
(3, 'G') -- not seen in #1 or #2 - return this record

SELECT *
  FROM #codes
  ORDER BY ID, Code

I need a query to return (2, 'E') and (3, 'G') because they did not occur in ID's prior to them.  Hope this makes sense.  Any help?

Thanks!


Viewing all articles
Browse latest Browse all 12890

Trending Articles



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