我想根据两个条件的匹配在表中查找重复的行。第一个条件,匹配“姓氏”字段中的数据。第二个条件,仅匹配FirstName字段中数据的前三个字符。
例如,应选择这两行:
Android Studio
我试图以RedFilter的解决方案为基础:Finding duplicate rows in SQL Server 但返回0条记录。这是我的查询;
synchronize
谢谢
已更新-这似乎可行
LastName FirstName
Williams Robert
Williams Robbie
答案 0 :(得分:0)
一个选择是仅获取一个COUNT()并按LastName和FirstName的前3个字符对其进行分区,然后进行过滤。
看看这个:
// this one of the methods which have errors
String __fastcall TAppVersion::GetFileVersion(void)
{
String Result ;
BufferPtr = NULL ;
// Get the product version.
wsprintf(QueryBlock, "\\StringFileInfo\\%04x%04x\\FileVersion",
Array[0].LanguageID, Array[0].CharacterSet);
VerQueryValue(MemPtr, QueryBlock, &BufferPtr, &BufferLength);
if(BufferPtr) Result = (char *)BufferPtr;
return(Result);
}
//---------------------------------------------------
__fastcall TAppVersion::TAppVersion()
{
FFileName = Application->ExeName ;
VerSize = GetFileVersionInfoSize(FFileName.c_str(), &VerInfo);
if (VerSize > 0) {
MemHandle = GlobalAlloc(GMEM_MOVEABLE, VerSize);
MemPtr = GlobalLock(MemHandle);
GetFileVersionInfo(FFileName.c_str(), VerInfo, VerSize, MemPtr);
VerQueryValue(MemPtr, "\\VarFileInfo\\Translation", &BufferPtr,
&BufferLength);
Array = (TransArray *)BufferPtr;
FFileVersion = GetFileVersion();
}
}
//-----------------------------------------------
答案 1 :(得分:0)
您可以按LEFT(FirstName,3)进行分组,例如:
declare @t table (firstName nvarchar(20), lastname nvarchar(20))
insert into @t
values ('Robert', 'Williams'), ('Robbie', 'Williams'), ('NotRob', 'Williams'), ('Steve', 'Other'), ('Steven', 'Other'), ('Someone', 'Else'), ('Roberto', 'Williams')
select t1.* from @t t1
cross apply (
select
LEFT(firstName, 3) as firstNameShort, lastname
from
@t t2
where LEFT(t2.firstName, 3) = LEFT(t1.firstName, 3)
and t2.lastname = t1.lastname
group by
lastname, LEFT(firstName, 3)
having
COUNT(*) > 1) t3
order by t1.lastname, t1.firstName
答案 2 :(得分:0)
这是我使用CTE和SUM()over()仅获取重复项的方法。
create table #example
(
LastName varchar(25)
,FirstName varchar(25)
)
INSERT INTO #example
VALUES
('Williams','Robert')
,('Williams','Robbie')
,('Jader','Arruda')
,('Jader','Gabriel')
WITH CTE
AS
(
SELECT FirstName, LastName,LEFT(FirstName,3) AS First_3_Letter_Name, SUM(1)
OVER(PARTITION BY LastName,LEFT(FirstName,3) ORDER BY
LastName,LEFT(FirstName,3)) AS ID
FROM #example
)
SELECT * FROM CTE AS a
WHERE ID > 1;