将两个oracle结果加入单个输出

时间:2016-04-14 19:56:28

标签: sql oracle

所有 我将两个查询转换为表格 table1如下:

SITE_ID LEGACY_PACKAGE  NEW_PACKAGE_MOVEMENT
1   CDTPMR  CDTPMRMX
1   CDTPMRNV    CDTPMRMX
1   CDTPMRU CDTPMRUMX
1   CDTPMRUNV   CDTPMRUMX
1   CONN100 CDTPMRMX
1   CONN100NVM  CDTPMRMX
1   CONNULD CONNULD
1   CONNULDNVM  CONNULD
1   ESSENTIALS  SIM3PKG
1   ESSENTLNVM  CDTESS
1   PHNE PREFR  CDTPMRMX

和表2如下:

SITE_ID PACKAGE_CODE    SERVICE_RATE
1   CDTESSU 0
1   CDTESSU 0
1   CDTESSU 0
1   CDTESSU 7.93
1   CDTESSU 12.99
1   CDTESSU -7.93
1   CDTESSU 8.99
1   CDTESSU 12.99
1   CDTESSU 0
1   CDTESSU 8.99
1   CDTESSU -5.99
1   CDTESSU 15.99
1   CDTESSU 0
1   CDTESSU 0
1   CDTESSU -5.99

我的新输出应该像

SITE_ID LEGACY_PACKAGE service_rate1    NEW_PACKAGE_MOVEMENT service_rate2

将legacy_package(table1)与package_code(table2)进行比较,以及service_rate1

 by comparing NEW_PACKAGE_MOVEMENT (table1) with package_code (table2) to service_rate2

I am trying different ways in oracle sql, but finding it harder.
is there any clue how to achieve this?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情..(未经测试)

    select 
    t1.site_id,t1.legacy_package,
    (case when t1.legacy_package=t2.package_code 
          then t2.service_rate else null end) as service_rate1,
    t1.new_package_movement,
    (case when t1.new_package_movement=t2.package_code
    then t2.service_rate else null end) as service_rate2
    from table1 t1 join 
    table t2 where t1.site_id=t2.site_id and 
   ((t1.legacy_package=t2.package_code) or (t2.new_package_movement=t2.package_code))