Anh Nguyen
Anh Nguyen

Reputation: 41

combine two select queries into one result

I have a query which has joined different tables and I got this result:

product_no       production_title       product_descr    sum(produce_quantity)
 10                bike                   bike              250
 20                mounter bike           moun-bike         300
 30                moto                   moto              400

And I have another SQL which selects from one table and returns a result like this:

product_ no       Sale_January   Sale_ Feb       ....          Sale_Dec
  10                  10            15                              20
  20                  12            0                               15

How can I join the results together? I would like to have a result like this:

  product_no  production_title   product_descr   sum(produce_quantity) sale_January .... sale_Dec
     10        bike                   bike              250                   10           20
     20        mounter bike           moun-bike         300                   12           15

Can anyone help me with this?

Upvotes: 0

Views: 44

Answers (3)

Stephen Wuebker
Stephen Wuebker

Reputation: 189

If I'm reading this correctly, you have two different queries that are returning the results above. You can use sub-queries to get what you need:

SELECT * FROM (
  <SQL for Query1>
) q1 INNER JOIN (
  <SQL for Query2>
) q2 ON q1.product_no = q2.product_no

Upvotes: 0

zip
zip

Reputation: 4061

Try this:

tb1: product_no       production_title       product_descr    sum(produce_quantity)

tb2: product_ no       Sale_January   Sale_ Freb       ....          Sale_Dec

Select tb1.product_no,  tb1.production_title,  tb1.product_descr,   tb1.sum(produce_quantity), tb2.*

from tb1 inner join tb2 on tb1.product_ no = tb2.product_ no

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269773

A simple join would seem to work:

select *
from result1 r1 join
     result2 r2
     using (product_no);

Upvotes: 1

Related Questions