如何在Oracle的SQL查询中使用从子查询返回的表

时间:2014-01-13 04:24:50

标签: sql database oracle oracle11g

我有一个像

这样的必需表格
+--------+----------+-----------------+--------------------+-------------------+
|offer_id|Offer_name|Total_offers_sold|total_device_changed|total_offer_changed|
+--------+----------+-----------------+--------------------+-------------------+

现在,对于前三列,我使用连接找到数据,然后按商品ID分组

+--------+----------+-----------------+
|offer_id|Offer_name|Total_offers_sold|
+--------+----------+-----------------+
|12      |abc       |23               |
+--------+----------+-----------------+
|23      |gdf       |3                |
+--------+----------+-----------------+
|54      |df        |54               |
+--------+----------+-----------------+
|56      |gf        |4                |
+--------+----------+-----------------+
|65      |ad        |17               |
+--------+----------+-----------------+
|75      |hg        |54               |
+--------+----------+-----------------+

对于其他两列,即offer_changed和total_device_changed,它们本身就是查找所需数据的复杂查询。让我说,我通过执行这两个列的查询找到了这个样本数据。

+--------+-------------------+
|offer_id|total_offer_changed|
+--------+-------------------+
|12      |3                  |
+--------+-------------------+
|56      |65                 |
+--------+-------------------+
|65      |4                  |
+--------+-------------------+

类似地,

+--------+--------------------+
|offer_id|total_device_changed|
+--------+--------------------+
|12      |2                   |
+--------+--------------------+
|23      |5                   |
+--------+--------------------+
|75      |20                  |
+--------+--------------------+

现在的问题是,这些是临时结果,我无法理解如何在对应于其商品ID的较大查询中合并此输出(表)的结果。我需要的最终结果是:

+--------+----------+-----------------+--------------------+-------------------+
|offer_id|Offer_name|Total_offers_sold|total_device_changed|total_offer_changed|
+--------+----------+-----------------+--------------------+-------------------+
|12      |abc       |23               |2                   |3                  |
+--------+----------+-----------------+--------------------+-------------------+
|23      |gdf       |3                |5                   |                   |
+--------+----------+-----------------+--------------------+-------------------+
|54      |df        |54               |                    |                   |
+--------+----------+-----------------+--------------------+-------------------+
|56      |gf        |4                |                    |65                 |
+--------+----------+-----------------+--------------------+-------------------+
|65      |ad        |17               |                    |04                 |
+--------+----------+-----------------+--------------------+-------------------+
|75      |hg        |54               |20                  |                   |
+--------+----------+-----------------+--------------------+-------------------+

请帮助

2 个答案:

答案 0 :(得分:0)

您正在寻找的是内联视图。内联视图是from子句中的子查询。与selectwhere子句中的子查询不同,您不能引用from子句中的其他表,但可以将内联查询的结果与主查询中的表一起加入。

在你的情况下,它将是:

select offer_id, offer_name, total_offers_sold
  from table_name t1
       inner join (select offer_id, total_offer_changed
                     from table_name
                    where whatever...
                   ) t2
            on t1.offer_id = t2.offer_id
 where whatever_the_other_conditions
;

但是,在现实世界中我会尝试做类似

的事情
select offer_id, offer_name, total_offers_sold, total_offer_changed
  from table_name
 where whatever_the_conditions
;

详细了解AskTom.comthis SO question上的内联视图。

答案 1 :(得分:0)

尝试此查询:

SELECT
t1.OFFER_ID,t1.OFFER_NAME,t1.TOTAL_OFFERS_SOLD,
t3.TOTAL_DEVICE_CHANGED,t2.TOTAL_OFFER_CHANGED 
FROM
TABLE1 t1
LEFT OUTER JOIN TABLE2 t2 ON t1.OFFER_ID = t2.OFFER_ID
LEFT OUTER JOIN TABLE3 t3 ON t1.OFFER_ID = t3.OFFER_ID 
ORDER BY t1.OFFER_ID;