根据不同表中的条件比较同一表中的2个值

时间:2018-08-17 09:42:23

标签: sql oracle

我想要的是检索所有ID,其中一种颜色的对应值高于另一种颜色的对应值。
例如,我想要“蓝色”的值大于ID匹配的“紫色”的值。

我有下表:
表1

ID  |   Color   |   KeyA    |   KeyB
1   |   Blue    |   AB      |   13
1   |   Green   |   AC      |   15
1   |   Purple  |   AG      |   56
2   |   Purple  |   DF      |   46
2   |   Pink    |   GH      |   67
3   |   Orange  |   GH      |   89
3   |   Green   |   YU      |   97

表2

KeyA    |   KeyB    |   Value       
AB      |   13      |   55      
DF      |   46      |   34      
YU      |   97      |   56      
DF      |   46      |   23      
AG      |   56      |   34      

在此示例中,结果将为1

仅使用的值:

表1

ID  |   Color   |   KeyA    |   KeyB
1   |   Blue    |   AB      |   13
1   |   Purple  |   AG      |   56

表2

KeyA    |   KeyB    |   Value       
AB      |   13      |   55      
AG      |   56      |   34      

2 个答案:

答案 0 :(得分:1)

在这种情况下,您应该使用两次t2表..应该是

select t1.ID 
from table1 t1 
inner join t12 on t1.color = 'Blue'  and t12.color ='Purple'
inner join  Table2 t2 ON t1.KeyA = t2.KeyA AND t1.KeyB = t2.KeyB 
inner join  Table2 t22 ON t1.KeyA = t22.KeyA AND t12.KeyB = t22.KeyB 
WHERE  t2.value > t22.value 

答案 1 :(得分:1)

您可以使用条件聚合:

select t1.id
from table1 t1
join table2 t2 on t2.keya = t1.keya and t2.keyb = t1.keyb
where t1.color in ('Blue', 'Purple')
group by t1.id
having max(case when t1.color = 'Blue' then t2.value end)
     > max(case when t1.color = 'Purple' then t2.value end);

使用CTE中的样本数据进行演示:

-- CTE to supply sample data for demo
with table1 (id, color, keya, keyb) as (
            select 1, 'Blue', 'AB', 13 from dual
  union all select 1, 'Green', 'AC', 15 from dual
  union all select 1, 'Purple', 'AG', 56 from dual
  union all select 2, 'Purple', 'DF', 46 from dual
  union all select 2, 'Pink', 'GH', 67 from dual
  union all select 3, 'Orange', 'GH', 89 from dual
  union all select 3, 'Green', 'YU', 97 from dual
),
table2(keya, keyb, value ) as (
            select 'AB', 13, 55 from dual
  union all select 'DF', 46, 34 from dual
  union all select 'YU', 97, 56 from dual
  union all select 'DF', 46, 23 from dual
  union all select 'AG', 56, 34 from dual
)
-- actual query
select t1.id
from table1 t1
join table2 t2 on t2.keya = t1.keya and t2.keyb = t1.keyb
where t1.color in ('Blue', 'Purple')
group by t1.id
having max(case when t1.color = 'Blue' then t2.value end)
     > max(case when t1.color = 'Purple' then t2.value end);

        ID
----------
         1

如果您不想重复颜色名称(并且实际上还没有以某种方式绑定它们),则可以在CTE或内联视图中提供它们:

select t1.id
from (
  select 1 as rnk, 'Blue' as color from dual
  union all select 2, 'Purple' from dual
) s
join table1 t1 on t1.color = s.color
join table2 t2 on t2.keya = t1.keya and t2.keyb = t1.keyb
group by t1.id
having max(case when s.rnk = 1 then t2.value end)
     > max(case when s.rnk = 2 then t2.value end);

如果您也想查看颜色及其值,则可以使用子查询和分析功能:

select id, color, value
from (
  select t1.id,
    t1.color,
    t2.value,
    max(case when t1.color = 'Blue' then t2.value end)
      over (partition by t1.id) as color1_value,
    max(case when t1.color = 'Purple' then t2.value end)
      over (partition by t1.id) as color2_value
  from table1 t1
  join table2 t2 on t2.keya = t1.keya and t2.keyb = t1.keyb
  where t1.color in ('Blue', 'Purple')
)
where color1_value > color2_value;

或使用内联视图再次删除重复项:

select id, color, value
from (
  select t1.id,
    t1.color,
    t2.value,
    max(case when s.rnk = 1 then t2.value end)
      over (partition by t1.id) as color1_value,
    max(case when s.rnk = 2 then t2.value end)
      over (partition by t1.id) as color2_value
  from (
    select 1 as rnk, 'Blue' as color from dual
    union all select 2, 'Purple' from dual
  ) s
  join table1 t1 on t1.color = s.color
  join table2 t2 on t2.keya = t1.keya and t2.keyb = t1.keyb
)
where color1_value > color2_value;

两者都给出:

        ID COLOR       VALUE
---------- ------ ----------
         1 Blue           55
         1 Purple         34