[英]SQL Server : Compare One Row with Multiple Rows of Same Table
I have data which is something like below: 我有如下数据:
id docid fname lname
1 1 x y
2 1 x y
3 1 x y
I need result of same document id with matching rows condition for docid 1 at the same time I want to compare id 3 with id 2 and 1 and get the matching record(s) only, but not for id 3. Something Like: 我需要同时具有docid 1匹配行条件的相同文档id的结果,我想将id 3与id 2和1进行比较,并且仅获取匹配的记录,而没有id 3的匹配记录。
id docid fname lname
1 1 x y
2 1 x y
Good news for me is comparing with single documentid only and I have that for example 1. Also, I have comparing record. 对我来说,好消息是仅与单个documentid进行比较,例如我有一个。同时,我也有比较记录。 Say id:3, which has to be compared with other two records. 说id:3,它必须与其他两个记录进行比较。
There are two cases below, consider compare id as 6: 以下是两种情况,请将比较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
If none of the records match, then empty is the result. 如果没有记录匹配,则结果为空。
I tried something like below: 我尝试了以下内容:
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
But, its failing when all the columns have null value(s). 但是,当所有列都具有空值时,它会失败。
Update : above is the sample one here is my real scenario table schema and data 更新:上面是示例之一,这是我的真实场景表架构和数据
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);
Try this : 尝试这个 :
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
You are defining the criteria in second line (select * from tbldoc where id = 3) as b
您在第二行中定义标准(select * from tbldoc where id = 3) as b
OR 要么
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
You are defining the criteria in last line where id = 3
您正在最后一行定义标准, where id = 3
--EDIT-- - 编辑 -
This is for your real 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.