Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to Join two - WITH Temp Tables

2817738May 25 2018 — edited May 28 2018

Team,

I have created two WITH temporary tables. Can anyone suggest them how to Join those two tables and call it in Select Query.

For Example

I need to join sum_sales1 and sum_sales2 and calling it in select query.

WITH

sum_sales1 AS

  ( select /*+ materialize */

    sum(quantity) all_sales from sales ),

number_stores AS

  ( select /*+ materialize */

    count(*) nbr_stores from store ),

sales_by_store AS

  ( select /*+ materialize */

  store_name, sum(quantity) store_sales from

  store natural join sales group by store_name)

SELECT

   store_name, store_sales, all_sales / nbr_stores avg_sales

FROM

   sum_sales,

   number_stores,

   sales_by_store

where

   store_sales > (all_sales / nbr_stores);

WITH

sum_sales2 AS

  ( select /*+ materialize */

    sum(quantity) all_sales from sales ),

number_stores AS

  ( select /*+ materialize */

    count(*) nbr_stores from store ),

sales_by_store AS

  ( select /*+ materialize */

  store_name, sum(quantity) store_sales from

  store natural join sales group by store_name)

SELECT

   store_name, store_sales, all_sales / nbr_stores avg_sales

FROM

   sum_sales,

   number_stores,

   sales_by_store

where

   store_sales > (all_sales / nbr_stores);

Thanks!

Kiran

This post has been answered by Frank Kulash on May 25 2018
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 25 2018
Added on May 25 2018
12 comments
3,083 views