简体   繁体   中英

Find similar strings in T-SQL table

I'm working on number plate recognition project. After number plate is recognized it is stored in MS SQL database. We have two cameras, and we get two images of one car, from front and from behind. Sometimes the recognizer recognize the number plate incorrectly from one of image. For example, the car with number plate 'AA1111' from first camera is recognized as 'AA111' and from second as 'AA1111'.

In my SQL table I have such records:

id NumberPlate Confidence CreatedAtTime
1 AA1111 100 13:44:00
2 AA111 75 13:44:10
3 BB2222 100 14:00:00
4 AA11 35 13:44:12

From every record in example table we create an incident in a client app. But this is incorrect because the plates 'AA11' and 'AA111' is the same plate as 'AA1111'.

My goal is to create incident in client app only for unique number, in my example it should be: 'AA1111' and 'BB2222'

Have you any ideas how to perform this in MS SQL?

The database is hosted in Azure SQL Server

UPD: I've write the SQL, but stuck with recursion limitation, may you hav some advice?


CREATE TABLE Plates(
  id INT not null,
  plate NVARCHAR(10),
  CreatedAtTime NVARCHAR(20),
  Confidence DECIMAL(18,5)
  )

INSERT INTO Plates
VALUES
(1,'LK2873','13:00:00',100),
(2,'LK287','13:00:10',70),
(3,'LK287','13:00:12',65),
(4,'AZ4875','14:00:00',100),
(5,'TR3345','14:15:32',100),
(6,'TR33','14:15:36',45),
(7,'TR334','14:15:40',70),
(8,'AA76','14:12:36',100),
(9,'DF324','14:13:00',100),
(9,'LK28','13:00:09',64)



;WITH tmp(plate,lvl,snd,ln)  as(
    SELECT Plate,1 lvl,soundex(Plate),LEN(Plate) FROM Plates 
        WHERE LEN(Plate)=(SELECT max(LEN(Plate)) FROM Plates)
    UNION ALL
    SELECT tb.plate, lvl+1,SOUNDEX(tb.Plate),LEN(tb.Plate)
        FROM Plates tb
        INNER JOIn tmp  t ON tb.Plate =LEFT(tb.plate,LEN(t.plate))

)
SELECT * FROM tmp

Think there is a lot of info needed to understand the full requirement. But assuming you do a lookup with the value on the table for anything existing and then insert.

Maybe something like this:

and 'AA11' representing the new value coming in.

WHERE 'AA11' = SUBSTRING(NumberPlate,1,LEN('AA11'))

thus would try and validate against anything with the same pattern up to its own length. so if nothing matches, it would be the first. if it matches, there is another with the same or more characters.

I'm not sure I understand your question, but if you are looking for any plate that is not the beginning of another plate, you could do it like:

select plate from plates p1
where not exists (
  select 1 from plates p2
  where p2.plate like p1.plate + '%'
    and p1.plate <> p2.plate 
);  

or

select plate from plates p1
where not exists (
  select 1 from plates p2
  where CHARINDEX(p2.plate, p1.plate) = 1
    and p1.plate <> p2.plate 
);  

See Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM