Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
How can i get distinct type_id, the query is getting almost 20k rows, most of them are duplicate type_id rows.I am doing a keyword search.select id,ocdata,tiffile,type,type_id from TAB_OcrData where ocdata LIKE ''%'+@KeyWord+'%''Thank you very much for the helpful info.
cplusplus
Aged Yak Warrior
567 Posts
Posted - 2011-03-01 : 11:25:02
I did the following using distinct with type_id , i am getting duplicate type_id rows in the resultset:select distinct type_id,ocdata,tiffile,type,id from TAB_OcrData where ocdata LIKE ''%'+@KeyWord+'%''Thank you very much for the helpful info.
Ifor
Aged Yak Warrior
700 Posts
Posted - 2011-03-01 : 11:35:53
You need to decide which row you want.The following selects the row with the highest id for each type_id.
;WITH dRNAS( SELECT [type_id], ocdata, tiffile, [type], id ,ROW_NUMBER() OVER (PARTITION BY [type_id] ORDER BY id DESC) AS RN FROM TAB_OcrData WHERE ocdata LIKE '%' + @KeyWord + '%')SELECT [type_id], ocdata, tiffile, [type], idFROM dRNWHERE RN = 1