根据两个表中其他列的匹配结果合并两列

时间:2018-06-20 12:52:14

标签: sql sql-server join

我有两个表,每个表包含两个相同的列,并且该表具有唯一的一列。我需要做的是合并这些表,并为每个匹配的相同列对生成唯一列的组合。我的意思示例:

ACC  ACTION  PRIORITY               ACC ACTION  TARGET
A    1       10                     A    1       i
A    2       15                     A    1       j
A    3       25                     A    3       k
B    3       101                    B    NULL    l
B    4       102                    B    4       m
B    5       103                    B    1       n

ACC和ACTION是两个表中的列。 ORDER对于左边的是唯一的,而TARGET对于右边的是唯一的。我需要在ACC和ACTION匹配的行上获得ORDER和TARGET的组合-例如,当ACC为A且ACTION为1时,PRIORITY为10,而TARGET为I或j,因此组合将为“ 10 I”和“ 10 j”。 另外,当右表中的ACTION为null时,该目标上应该有一行具有最高PRIORITY的行。 因此,预期结果:

PRIORITY TARGET
10       i
10       j
25       k
102      m
103      l

任何尝试进行正确JOIN的尝试都在我身边失败了。

我尝试过的事情:

INSERT INTO @RESULT(TARGET, PRIORITY)
SELECT R.TARGET, MAX(L.PRIORITY)
FROM LEFT_TABLE L INNER JOIN RIGHT_TABLE R 
ON L.ACC=R.ACC AND (L.ACTION = R.ACTION OR R.ACTION IS NULL);

但是它给出了一个错误。但是,按TARGET分组并不能产生正确的输出。

4 个答案:

答案 0 :(得分:1)

我用UNION解决了这个问题

SELECT priority, t.target
FROM prio p
JOIN target t ON p.acc = t.acc AND t.action = p.action
UNION
SELECT priority, t.target
FROM prio p
JOIN target t ON p.acc = t.acc AND t.action is null
AND p.priority = (SELECT MAX(priority) FROM prio)

答案 1 :(得分:0)

查看是否可行。我没有测试。

DECLARE @L TABLE(ACC NVARCHAR(10),Action INT,Priority INT)
INSERT @L (ACC,Action,Priority) VALUES ('A',1,10),('A',2,15),('A',3,25),('B',4,101),('B',5,102),('B',6,103)

DECLARE @R TABLE(ACC NVARCHAR(10),Action INT,Target NVARCHAR(10))
INSERT @R (ACC,Action,Target) VALUES ('A',1,'i'),('A',1,'j'),('A',3,'k'),('B',NULL,'l'),('B',4,'m'),('B',1,'n')

SELECT
    Target = MAX(R.Target),
    Priority = MAX(L.Priority)
FROM
    @L L
    INNER JOIN @R R ON R.ACC=L.Acc AND (R.ACTION=L.Action OR R.Action IS NULL)
GROUP BY
    L.ACC,
    L.Action
ORDER BY
     MAX(L.Priority)

答案 2 :(得分:0)

您可以这样做:

样本数据

create table a
(
acc nvarchar(50),Action1 int, priority1 int)

create table b (acc nvarchar(50),action1 int, target1 nvarchar(50)
)

insert into a
values 

('A',1, 10 ),                  
('A',2, 15 ),                  
('A',3, 25 ),                  
('B',3, 101),                  
('B',4, 102),                  
('B',5, 103)

 insert into dbo.b
 values

('A',1,'i'),
('A',1,'j'),
('A',3,'k'),
('B',null,'l'),
('B',4,'m'),
('B',1,'n')  

SQL

with data1 as (
select acc,case when action1 is null then maxaction1 else action1 end as action1,target1 from (
select * from dbo.b a
cross apply (select MAX(action1) as MaxAction1 from dbo.a b where a.acc = b.acc ) x
)z
)

select priority1,target1 from data1 a inner join dbo.a b on a.acc = b.acc and a.action1 = b.Action1

更新 刚刚看到您在“优先级”上写了您想要的内容。然后您可以这样做:

SQL代码更新

with data1 as (
select * from (
select y.acc,y.target1,COALESCE(y.action1,c.action1) as Action1 from (
select * from dbo.b a
cross apply (select MAX(priority1) as MaxP from dbo.a b where a.acc = b.acc ) x
)y inner join dbo.a c on c.priority1 = MaxP
)z
)

select priority1,target1 from data1 a inner join dbo.a b on a.acc = b.acc and a.action1 = b.Action1

结果

enter image description here

答案 3 :(得分:0)

您可以尝试以下方法:

permission:  "io.k8s.core.v1.pods.exec.get"     
resource:  "core/v1/namespaces/default/pods/pubsub-a5a21f14-0bd1-4338-87b1-8658c3bbc7ad-9gm4n-8nz14/exec"