连接表和在Oracle SQL中获取数据的麻烦

时间:2014-08-01 21:34:15

标签: sql oracle select case outer-join

我正在使用Oracle SQL Developer来运行我的查询。

我在Oracle数据库中有两个表。见下文。

                  table_1
------------------------------------------------------
    SKU    |  MAIN_LOCATION   |   BACKUP_LOCATION
------------------------------------------------------
    111    |     05-AA        |      09-DD
    222    |     02-BB        |      10-JJ
    333    |     07-CC        |      13-LL
    444    |     06-HH        |      19-PP


                  table_2
---------------------------------------------
    SKU    |    QUANTITY    |    LOCATION
---------------------------------------------
    111    |        5       |     05-AA
    111    |        8       |     09-DD
    222    |        2       |     02-BB
    333    |        4       |     07-CC
    444    |        1       |     06-HH
    444    |        11      |     19-PP
    555    |        16      |     21-UU



我需要什么
我需要的是一个查询,显示table_1上的所有SKU(每行一个SKU),以及根据table_2位置的列数量。如果备份位置中没有提供数据,那么我只想显示一个空值。我需要的例子如下。

                   QUERY NEEDED
---------------------------------------------------------------------------
  SKU  |  MAIN_LOC  |   MAIN_LOC_QTY  |   BACKUP_LOC   |   BACKUP_LOC_QTY
---------------------------------------------------------------------------
  111  |     05-AA  |       5         |    09-DD       |      8
  222  |     02-BB  |       2         |    10-JJ       |    (null)
  333  |     07-CC  |       4         |    07-CC       |    (null)
  444  |     06-HH  |       1         |    19-PP       |      11



我尝试过什么
我首先运行了一个查询,以确保我能够从table_1提取所需的所有SKU。

SELECT   table_1.sku AS SKU,
         table_1.main_location AS MAIN_LOC

FROM     table_1


没问题。

然后我将table_2添加到查询中,甚至没有添加任何列,只是为了确保我获得的SKU行数量与之前的查询相同,这不起作用。见下文。

SELECT   table_1.sku AS SKU,
         table_1.main_location AS MAIN_LOC

FROM     table_1,
         table_2

WHERE    table_1.sku = table_2.sku(+)

这是我开始遇到问题的时候;如果SKU存在于table_2中的两行,那么它在我的查询中显示为两行,如下所示:

  SKU  |  MAIN_LOC
--------------------
  111  |  09-DD
  111  |  09-DD
  222  |  02-BB
  333  |  07-CC
  444  |  06-HH
  444  |  06-HH

我需要上面的每个SKU仍然只显示一行。

假设上述查询甚至有效,我仍然试图根据位置获取数量信息,如下所示:

SELECT   table_1.sku AS SKU,
         table_1.main_location AS MAIN_LOC,
         CASE
           WHEN table_1.main_location = table_2.location THEN table2.quantity
           ELSE NULL
         END AS MAIN_LOC_QUANTITY,
         table_1.backup_location AS BACKUP_LOC,
         CASE
           WHEN table_1.backup_location = table_2.location THEN table2.quantity
           ELSE NULL
         END AS BACKUP_LOC_QUANTITY

FROM     table_1,
         table_2

WHERE    table_1.sku = table_2.sku(+)


加入/外连接(+)是获得我需要的所有SKU的唯一方法,但仍然最终获得相同SKU的重复行(这是我不想要的)。

我甚至试图在FROM语句中创建一个子查询来减少我不需要的一些SKU,如下所示:


SELECT   table_1.sku AS SKU,
         table_1.main_location AS MAIN_LOC,
         CASE
           WHEN table_1.main_location = sub_table.location THEN sub_table.quantity
           ELSE NULL
         END AS MAIN_LOC_QUANTITY,
         table_1.backup_location AS BACKUP_LOC,
         CASE
           WHEN table_1.back_location = sub_table.location THEN sub_table.quantity
           ELSE NULL
         END AS BACKUP_LOC_QUANTITY

FROM     table_1,
         (
          SELECT  *

          FROM    table_2

          WHERE   location NOT LIKE '%[SOME CONDITIONS]%'
         ) sub_table

WHERE    table_1.sku = table_2.sku


这产生与先前查询几乎相同的结果。它摆脱了我不需要的一些东西,但没有解决主要问题。

我还在绝望中添加了一些我对连接/外连接知之甚少的内容:

WHERE    table_1.sku(+) = table_2.sku

以及:

WHERE    table_1.sku = table_2.sku(+)


但是在这一点上,我认为我只是抓住吸管而且我自己充斥着太多我不知道的东西。

我的问题
A)我无法弄清楚如何让MAIN_LOC_QTY列根据Table 2数据显示数量。

B)当来自Table 1的SKU存在于两行Table 2上时,我的查询为一个SKU生成两行,当我只需要一行存在与该SKU有关的所有信息时。

任何帮助将不胜感激。谢谢!


我的解决方案(2014年2月2日更新)
对于遇到此问题的人,我的解决方案由Brian Demilia提供。基本上,我的查询如下;

SELECT   table_1.sku,              --This is from table_1
         table_1.main_location,    --This is from table_1
         main_loc.quantity,        --This is from the 1st LEFT JOIN named main_loc
         table_1.backup_location,  --This is from table_1
         backup_loc.quantity       --This is from the 2nd LEFT JOIN named backup_loc

FROM     table_1
           /*LEFT JOIN below is to make table_2 with only the main_location from table_1*/
           LEFT JOIN table_2 main_loc
             ON  table_1.main_location = main_loc.location
             AND table_1.sku = main_loc.sku
           /*LEFT JOIN below is to make table_2 with only the backup_location from table_1*/
           LEFT JOIN table_2 backup_loc
             ON  table_1.backup_location = backup_loc.location
             AND table1.sku = backup_loc.sku


我希望以上帮助遇到此问题的其他人。感谢所有回复。

- 安东尼

1 个答案:

答案 0 :(得分:3)

select t1.sku,
       t1.main_location,
       ml.quantity        as main_qty,
       t1.backup_location,
       bl.quantity        as bkup_qty
  from table_1 t1
  left join table_2 ml
    on t1.sku = ml.sku
   and t1.main_location = ml.location
  left join table_2 bl
    on t1.sku = ml.sku
   and t1.backup_location = bl.location
 order by t1.sku

小提琴:http://sqlfiddle.com/#!4/ddbe9/4/0