Skip to Main Content
  • Questions
  • how to join columns from multiple queries...

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Nagaraj.

Asked: February 12, 2016 - 10:55 am UTC

Last updated: February 12, 2016 - 11:03 am UTC

Version: 12.2

Viewed 1000+ times

You Asked

I have multiple queries that gives different results for same product.

for.

One query returns following rows and columns:-
---------------------------------------------


Product Total sales(K units)
category1 30
category2 40
category3 50



Second query returns following
------------------------------

Product Incentives given(in 1000$)
category1 1500
category2 600
category3 400

-----------


This is difficult put this in single query because of different condition used in each query.

From 2 queries I want to get one result like below:-
-------------------------------------------------------

Product Total sales(K units) Incentives given(in 1000$)
category1 30 1500
category2 40 600
category3 50 400


-----------------


Is there a way to do this without using temporary table ?



and Connor said...

Assuming you already *have* those two queries, just bring them together using a WITH clause, eg

with 
  query1 as ( select .... ),   -- your existing query1
  query2 as ( select .... )   -- your existing query2
select *
from query1, query2
where query1.product = query2.product;


The optimizer will sort it all out for you.

Is this answer out of date? If it is, please let us know via a Comment

More to Explore

Performance

Get all the information about database performance in the Database Performance guide.