Query to find sum of grouped counts from 2 tables

From: "Satish Burnwal (sburnwal)" <sburnwal(at)cisco(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Query to find sum of grouped counts from 2 tables
Date: 2011-01-07 10:15:25
Message-ID: 3A8C969225424C4D8E6BEE65ED8552DA036B2F55@XMB-BGL-41C.cisco.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have 2 tables containing the data for same items:

STORE1
-----------------------------
Id type items
-----------------------------
1 FOOD 10
2 FOOD 15
3 SOAP 20

STORE2
-----------------------------
Id type items
-----------------------------
1 FOOD 15
3 SOAP 10
4 PAPER 25
5 SOAP 12

What I am looking for is one single query that would return me TYPE-wise
total number of items from both the tables. UNION does not help me. I
want the result as:

Type count
-----------------------
FOOD 40 //10+15+15
SOAP 42 //20+10+12
PAPER 25

Thanks in advance,
-Satish

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Kellerer 2011-01-07 10:28:43 Re: Query to find sum of grouped counts from 2 tables
Previous Message Benjie Buluran 2011-01-07 08:35:56 Re: Inserting data from one database to another using stored functions