SQL 服务器:比较一行与多行相同 Table
SQL Server : Compare One Row with Multiple Rows of Same Table
我有如下数据:
id docid fname lname
1 1 x y
2 1 x y
3 1 x y
我需要具有匹配行的相同文档 ID 的结果
同时 docid 1 的条件我想将 id 3 与 id 2 和 1 进行比较并仅获取匹配的记录,而不是 id 3。类似:
id docid fname lname
1 1 x y
2 1 x y
对我来说好消息是仅与单个 documentid 进行比较,例如 1。另外,我有比较记录。比如说id:3,这要和另外两条记录比较。
有以下两种情况,考虑比较id为6:
id docid fname lname
4 2 p q
5 2 r s
6 2 p q
id docid fname lname
4 2 p q
如果 none 条记录匹配,则结果为空。
我试过如下:
SELECT ta.id,ta.docid,ta.fname,ta.lname FROM tbldoc ta
WHERE (SELECT COUNT(*)FROM tbldoc ta2
WHERE ISNULL(ta.id,'') = ISNULL(ta2.id,'') AND
ISNULL(ta.docid,'') = ISNULL(ta2.docid,'') AND
ISNULL(ta.fname,'') = ISNULL(ta2.fname ,'') AND
ISNULL(ta.lname,'') = ISNULL(ta2.lname ,'')
)>1 AND docid=1 And id<>3
但是,当所有列都为空值时,它会失败。
更新:上面是示例
这是我的真实场景 table 架构和数据
create table tbldoc (Created int,Checkn nvarchar(max),Account nvarchar(max),EONumber nvarchar(max),Voucher nvarchar(max),Invoice nvarchar(max),Total decimal,Venue nvarchar(max),Reference nvarchar(max),Sign bit,Room nvarchar(max),Page int);
insert into tbldoc values(59,1234,NULL,NULL,NULL,NULL,40,3,NULL,1,NULL,NULL);
insert into tbldoc values(62,1234,NULL,NULL,NULL,NULL,40,3,NULL,1,NULL,NULL);
insert into tbldoc values(68,1234,NULL,NULL,NULL,NULL,40,3,NULL,1,NULL,NULL);
试试这个:
select a.* from tbldoc as a right join
(select * from tbldoc where id = 3) as b
on a.docid = b.docid
and a.fname = b.fname
and a.lname = b.lname
and a.id != b.id
您正在第二行定义条件(select * from tbldoc where id = 3) as b
或
select a.*
from tbldoc as a right join tbldoc as b
on a.docid = b.docid
and a.fname = b.fname
and a.lname = b.lname
and a.id != b.id
where b.id = 3
您在最后一行定义条件 where id = 3
--编辑--
这是给你真正的 table :
select a.*
from tbldoc as a right join tbldoc as b
on (a.checkn = b.checkn or b.checkn is NULL)
and (a.Account = b.Account or b.Account is NULL)
and (a.EONumber = b.EONumber or b.EONumber is NULL)
and (a.Invoice = b.Invoice or b.Invoice is NULL)
and (a.Total = b.Total or b.Total is NULL)
and (a.Venue = b.Venue or b.Venue is NULL)
and (a.Reference = b.Reference or b.Reference is NULL)
and (a.Sign = b.Sign or b.Sign is NULL)
and (a.Room = b.Room or b.Room is NULL)
and (a.Page = b.Page or b.Page is NULL)
and a.created != b.created
where b.created = 68
我有如下数据:
id docid fname lname
1 1 x y
2 1 x y
3 1 x y
我需要具有匹配行的相同文档 ID 的结果 同时 docid 1 的条件我想将 id 3 与 id 2 和 1 进行比较并仅获取匹配的记录,而不是 id 3。类似:
id docid fname lname
1 1 x y
2 1 x y
对我来说好消息是仅与单个 documentid 进行比较,例如 1。另外,我有比较记录。比如说id:3,这要和另外两条记录比较。
有以下两种情况,考虑比较id为6:
id docid fname lname
4 2 p q
5 2 r s
6 2 p q
id docid fname lname
4 2 p q
如果 none 条记录匹配,则结果为空。
我试过如下:
SELECT ta.id,ta.docid,ta.fname,ta.lname FROM tbldoc ta
WHERE (SELECT COUNT(*)FROM tbldoc ta2
WHERE ISNULL(ta.id,'') = ISNULL(ta2.id,'') AND
ISNULL(ta.docid,'') = ISNULL(ta2.docid,'') AND
ISNULL(ta.fname,'') = ISNULL(ta2.fname ,'') AND
ISNULL(ta.lname,'') = ISNULL(ta2.lname ,'')
)>1 AND docid=1 And id<>3
但是,当所有列都为空值时,它会失败。
更新:上面是示例 这是我的真实场景 table 架构和数据
create table tbldoc (Created int,Checkn nvarchar(max),Account nvarchar(max),EONumber nvarchar(max),Voucher nvarchar(max),Invoice nvarchar(max),Total decimal,Venue nvarchar(max),Reference nvarchar(max),Sign bit,Room nvarchar(max),Page int);
insert into tbldoc values(59,1234,NULL,NULL,NULL,NULL,40,3,NULL,1,NULL,NULL);
insert into tbldoc values(62,1234,NULL,NULL,NULL,NULL,40,3,NULL,1,NULL,NULL);
insert into tbldoc values(68,1234,NULL,NULL,NULL,NULL,40,3,NULL,1,NULL,NULL);
试试这个:
select a.* from tbldoc as a right join
(select * from tbldoc where id = 3) as b
on a.docid = b.docid
and a.fname = b.fname
and a.lname = b.lname
and a.id != b.id
您正在第二行定义条件(select * from tbldoc where id = 3) as b
或
select a.*
from tbldoc as a right join tbldoc as b
on a.docid = b.docid
and a.fname = b.fname
and a.lname = b.lname
and a.id != b.id
where b.id = 3
您在最后一行定义条件 where id = 3
--编辑--
这是给你真正的 table :
select a.*
from tbldoc as a right join tbldoc as b
on (a.checkn = b.checkn or b.checkn is NULL)
and (a.Account = b.Account or b.Account is NULL)
and (a.EONumber = b.EONumber or b.EONumber is NULL)
and (a.Invoice = b.Invoice or b.Invoice is NULL)
and (a.Total = b.Total or b.Total is NULL)
and (a.Venue = b.Venue or b.Venue is NULL)
and (a.Reference = b.Reference or b.Reference is NULL)
and (a.Sign = b.Sign or b.Sign is NULL)
and (a.Room = b.Room or b.Room is NULL)
and (a.Page = b.Page or b.Page is NULL)
and a.created != b.created
where b.created = 68