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

Custom, partial distinct

$
0
0

Hello,

I have following table which has repeated codes in the Code column for each names.

How can I eliminate the rows justconsequently duplicated codes for each names?

CREATE TABLE #T
(
Name nvarchar(50)
,ID int
,Dt datetime
,Code  nvarchar(50)
)

INSERT INTO #T
SELECT 'NameA',1, '1.1.2013 00:00','Code1'
UNION SELECT   'NameA',2, '1.1.2013 01:00','Code1'
UNION SELECT   'NameA',3, '1.1.2013 02:00','Code2'
UNION SELECT   'NameA',4, '1.1.2013 03:00','Code2'
UNION SELECT   'NameA',5, '1.1.2013 04:00','Code1'
UNION SELECT   'NameA',6, '1.1.2013 05:00','Code1'
UNION SELECT   'NameB',7, '1.1.2013 00:00','Code1'
UNION SELECT   'NameB',8, '1.1.2013 01:00','Code1'
UNION SELECT   'NameB',9, '1.1.2013 02:00','Code2'
UNION SELECT   'NameB',10, '1.1.2013 03:00','Code2'
UNION SELECT   'NameB',11, '1.1.2013 04:00','Code1'
UNION SELECT   'NameB',12, '1.1.2013 05:00','Code1'
select Name,dt,ID,Code from #T  group by Name,dt,ID,code
DROP TABLE #T

 I need to get following output that eliminates the rows with IDs: 2-4-6 (for name A)  8-10-12 (for name B)

NameA----01.01.2013 00:00:00----1----Code1



NameA----01.01.2013 02:00:00----3----Code2




NameA----01.01.2013 04:00:00----5----Code1




NameB----01.01.2013 00:00:00----7----Code1




NameB----01.01.2013 02:00:00----9----Code2




NameB----01.01.2013 04:00:00----11----Code1




thank you...




Viewing all articles
Browse latest Browse all 23857

Trending Articles



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