在一列中连接两个具有不同值的表

时间:2018-05-01 01:53:19

标签: sql oracle join pivot

我不是sql的专家。 我有一些如下数据:

select column1,column2,cloumn3 from table1;

----------------------------------------------

sub1  2207   43
sub2  2207   45
sub3  2207   47

select column1,column2,cloumn3,attribute_name,attribute_value from table2;
--------------------------------------------------------------------------

sub1  2207   43  ENTITY_ID          VIJAY
sub2  2207   45  ENTITY_ID          SHANKAR
sub2  2207   45  ACTIVATION_DATE    01052018
sub3  2207   47  ENTITY_ID          RISHI
sub3  2207   47  ACTIVATION_DATE    01042018

我需要一个输出:

sub1  2207   43   VIJAY       NULL   
sub2  2207   45   SHANKAR     01052018
sub3  2207   47   RISHI       01042018

这对我来说似乎很难。任何人都可以帮我找到查询。

3 个答案:

答案 0 :(得分:1)

两个连接怎么样?

select t1.*, t2e.attribute_value, t2d.attribute_value
from table1 t1 left join
     table2 t2e
     on t1.col1 = t22.col1 and t1.col2 = t2e.col2 and t1.col3 = t2e.col3 and
        t2e.attribute_name = 'ENTITY_ID' left join
     table2 t2d
     on t1.col1 = t22.col1 and t1.col2 = t2e.col2 and t1.col3 = t2e.col3 and
        t2e.attribute_name = 'ACTIVATION_DATE';

或者,因为您似乎不需要table1中的任何内容,您可以使用group by

select col1, col2, col3,
       max(case when attribute_name = 'ENTITY_ID' then attribute_value end),
       max(case when attribute_name = 'ACTIVATION_DATE' then attribute_value end)
from table2
group by col1, col2, col3;

答案 1 :(得分:1)

一种选择是使用子查询执行类似的操作:

select column1,column2,cloumn3
 , (
   select attribute_value
   from table2 b
   where attribute_name = 'ENTITY_ID'
     and a.column1 = b.column1
     and a.column2 = b.column2
     and a.column3 = b.column3
 )
 , (
   select attribute_value
   from table2 b
   where attribute_name = 'ACTIVATION_DATE'
     and a.column1 = b.column1
     and a.column2 = b.column2
     and a.column3 = b.column3
 )
from table1 a;

其他选项,更漂亮,可能具有更好的性能,具体取决于数据库引擎将使用第二个表的一个轴,然后将其与第一个表连接。但如果你有兴趣,我会让你调查它。

答案 2 :(得分:1)

您可以使用PIVOT运算符 - 只有table2是绝对必要的:

SELECT * FROM (
    SELECT column1, column2, column3, ..., attribute_name, attribute_value
      FROM table2
) PIVOT (
    MAX(attribute_value)
    FOR attribute_name IN ('ENTITY_ID' AS entity_id, 'ACTIVATION_DATE' AS activation_date)
)
 ORDER BY column1;

See working SQL Fiddle here

请注意,MAX()中的PIVOT仅在那里,因为PIVOT需要聚合函数。 MIN()也可以正常工作(只有一个值)。

希望这有帮助。

编辑:刚刚看到您对table1中许多其他列的评论。在这种情况下,您需要以下内容:

SELECT * FROM (
    SELECT t1.column1, t1.column2, t1.column3, ..., t2.attribute_name, t2.attribute_value
      FROM table1 t1 INNER JOIN table2 t2
        ON t1.column1 = t2.column1
       AND t1.column2 = t2.column2
       AND t1.column3 = t2.column3
) PIVOT (
    MAX(attribute_value)
    FOR attribute_name IN ('ENTITY_ID' AS entity_id, 'ACTIVATION_DATE' AS activation_date)
)
 ORDER BY column1;

编辑#2 :如果您需要将ACTIVATION_DATE转换为实际日期,您可以执行以下操作:

SELECT column1, column2, column3, ..., entity_id
     , TO_DATE(activation_date, 'ddmmyyyy') AS activation_date
  FROM (
    SELECT t1.column1, t1.column2, t1.column3, ..., t2.attribute_name, t2.attribute_value
      FROM table1 t1 INNER JOIN table2 t2
        ON t1.column1 = t2.column1
       AND t1.column2 = t2.column2
       AND t1.column3 = t2.column3
) PIVOT (
    MAX(attribute_value)
    FOR attribute_name IN ('ENTITY_ID' AS entity_id, 'ACTIVATION_DATE' AS activation_date)
)
 ORDER BY column1;

但如果您将日期存储为字符串,则转换可能不是100%安全。