简体   繁体   English

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

[英]How to use tables returned from subquery in SQL query in Oracle

I have a required table like 我有一个必填表,例如

+--------+----------+-----------------+--------------------+-------------------+
|offer_id|Offer_name|Total_offers_sold|total_device_changed|total_offer_changed|
+--------+----------+-----------------+--------------------+-------------------+

now, For first three columns I have found the data using joins and then grouping by offer ID 现在,对于前三列,我使用联接找到了数据,然后按商品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               |
+--------+----------+-----------------+

For other two columns ie offer_changed and total_device_changed, they are themselves complex queries to find the required data. 对于其他两列(即offer_changed和total_device_changed),它们本身是复杂的查询,用于查找所需的数据。 lets, say I have this sample data found by executing query for those two clumns. 假设我通过执行针对这两个笨拙的查询找到了此样本数据。

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

similarly, 同样

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

Now the problem is, these are temporary results and I am unable to understand how to merge the results of this output(tables) in the bigger query corresponding to their offer ID's.ie the final result I need is : 现在的问题是,这些是临时结果,我无法理解如何在与商品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                  |                   |
+--------+----------+-----------------+--------------------+-------------------+

please help 请帮忙

The thing that you are looking for is an inline view. 您正在寻找的是嵌入式视图。 An inline view is a subquery in from clause. 内联视图是from子句中的子查询。 Unlike subqueries in select and where clauses, you cannot refer to the other tables in your from clause, but you can join the result of the inline query with the tables in your main query. selectwhere子句中的子查询不同,您不能在from子句中引用其他表,但是可以将内联查询的结果与主查询中的表联接。

In your case it would be: 您的情况是:

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
;

But, in real world I would simply try to do something like 但是,在现实世界中,我只会尝试做类似的事情

select offer_id, offer_name, total_offers_sold, total_offer_changed
  from table_name
 where whatever_the_conditions
;

Read more about inline views on AskTom.com and this SO question . AskTom.com上阅读有关内联视图的更多信息以及此SO问题

try this query: 试试这个查询:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM