你可以尝试这样的事情:
select *
from
(
SELECT A.val1,A.val2, B.val1, B.val2, B.val3, C.val1, C.val2
, row_number() over
(
partition by b.val3
order by A.val1, A.val2, B.val1, B.val2, C.val1, C.val2
) r
FROM A
INNER JOIN B ON A.val1 = B.val1
INNER JOIN C ON A.val1 = C.val1
) x
where x.r = 1
或者
SELECT max(A.val1)
,max(A.val2)
, max(B.val1)
, max(B.val2)
, B.val3
, max(C.val1)
, max(C.val2)
FROM A
INNER JOIN B ON A.val1 = B.val1
INNER JOIN C ON A.val1 = C.val1
group by b.val3
取决于您要达到的目标。如果那些不做你所追求的,请你能提供更多关于你希望做什么/示例数据的信息吗?
您所拥有的问题是选择Distinct B.Val3时,可能有多个记录关联:
- 表 B 中 V3 列中的值是否唯一?
- 表 A 和/或 C 的 V1 列中的值是否唯一?
如果上述任何一个问题的答案是否定的,您需要为 SQL 提供一种方法来决定在选择要为其他列显示哪些数据时选择多个可能的记录/结果中的哪一个。
编辑
根据上面给出的示例数据,请找到一个脚本来复制示例信息并显示解决方案:
if object_id('a') is not null drop table a
if object_id('b') is not null drop table b
if object_id('c') is not null drop table c
go
create table b
(
val1 int not null identity(100,1) primary key clustered
, val2 nvarchar(2) not null
, val3 nvarchar(3) not null
)
go
create table c
(
val1 int not null identity(200,1) primary key clustered
, val2 nvarchar(2) not null
)
go
create table a
(
val1 int not null identity(1,1) primary key clustered
, val2 int not null constraint fk_a_b foreign key references b(val1)
, val3 int not null constraint fk_a_c foreign key references c(val1)
)
go
--ids 100 - 105
insert b
select 'a2', 'aaa'
union all select 'b2', 'bbb'
union all select 'c2', 'ccc'
union all select 'c3', 'ccc' --val3 is not unique
union all select 'c4', 'ccc' --
union all select 'b3', 'bbb' --
--ids 200 - 204
insert c
select 'a3'
union all select 'b3'
union all select 'c3'
union all select 'd3'
union all select 'e3'
insert a
select 100, 200
union all select 100, 200
union all select 100, 201
union all select 101, 200
union all select 102, 200
union all select 102, 201
union all select 103, 201
union all select 104, 201
union all select 105, 201
union all select 105, 202
union all select 105, 203
union all select 105, 204
--what does the full result set look like?
SELECT A.val1 aval1
, A.val2 aval2
, B.val1 bval1
, B.val2 bval2
, B.val3 bval3
, C.val1 cval1
, C.val2 cval2
FROM A
INNER JOIN B
ON A.val2 = B.val1
INNER JOIN C
ON A.val3 = C.val1
--now show unique B's
select Aval1, Aval2, Bval1, Bval2, Bval3, Cval1, Cval2
from
(
SELECT A.val1 aval1
, A.val2 aval2
, B.val1 bval1
, B.val2 bval2
, B.val3 bval3
, C.val1 cval1
, C.val2 cval2
, row_number() over
(
partition by b.val3
order by b.val1, c.val1 --try playing with this to see how the results change / see what fits your requirements
) r
FROM A
INNER JOIN B
ON A.val2 = B.val1
INNER JOIN C
ON A.val3 = C.val1
) x
where x.r = 1
--what wasn't included in the unique B result set, but was in the full set?
select Aval1, Aval2, Bval1, Bval2, Bval3, Cval1, Cval2
from
(
SELECT A.val1 aval1
, A.val2 aval2
, B.val1 bval1
, B.val2 bval2
, B.val3 bval3
, C.val1 cval1
, C.val2 cval2
, row_number() over
(
partition by b.val3
order by b.val1, c.val1 --try playing with this to see how the results change / see what fits your requirements
) r
FROM A
INNER JOIN B
ON A.val2 = B.val1
INNER JOIN C
ON A.val3 = C.val1
) x
where x.r > 1