Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
select duplicate record where substring of one row is equal to substring of another row
using partition by query ex. SN2065= SN2065#1 or SN2065= SN2065#2
Posted

1 solution

SQL
with cte
as
(
select ID from TableName
Group By ID
Having Count(SUBSTRING(ColumnName,StartIndex,EndIndex))>1
)
Select * from TableName s,cte c where s.ID=c.ID and s.ID='YourID'



Hope it will help you
 
Share this answer
 
Comments
aparnaChandras 21-Aug-14 3:24am    
I have use following query but it will not work , how to modify the same query ?
;with CTE_data
([Seq],
[ENGINENO],
[HULLNO],
[EngineType],
[ENO],
[HNO],
[Etype],
[Licensee],
[Engine_OWNER],
[YARD],
[DLDATE],
cn,
RowNumber
)
AS
(
SELECT a.[Seq],
a.[ENGINENO],
a.[HULLNO],
a.[EngineType],
a.[ENO],
a.[HNO],
a.[Etype],
a.[Licensee],
a.[Engine_OWNER],
a.[YARD],
a.[DLDATE],
COUNT(*)over (PARTITION by [HULLNO]+'%')

,
RowNumber = ROW_NUMBER() over (PARTITION by [HULLNO]+'%' order by a.[Seq])

FROM APDE_6Aug.[dbo].[EngineMaster_Duplicates] a where
(
[HULLNO] like SUBSTRING(a.[HULLNO],1,LEN([HULLNO]))
)
and LEN([HULLNO])>1
)
Select * from CTE_data where Cn>2
Salman622 21-Aug-14 3:30am    
what value is there in hullno
is it SN2065 or (SN2065#1 ....)
and what is the primary key of table EngineMaster_Duplicates
aparnaChandras 21-Aug-14 3:35am    
both values are there in different row in same column
aparnaChandras 21-Aug-14 3:36am    
Seq ENGINENO HULLNO
55525 HYUNAA5344 SN2066
55526 HYUNAA5345 SN2066
105793 AA5344 SN2066
105795 AA5344 SN2066
113532 NULL SN2066 # 1
113533 NULL SN2066 # 2

I have to show that these are duplicates
Salman622 21-Aug-14 3:45am    
with Cte
as
(
select HULLNO from EngineMaster_Duplicates
Group By HULLNO
Having Count(SUBSTRING(HULLNO,1,6))>1
)
Select * from EngineMaster_Duplicates s,cte c where s.HULLNO =c.HULLNO and s.VoucherNo Like 'SN2066%'


try this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900