| From: | Thomas Kellerer <spam_eater(at)gmx(dot)net> |
|---|---|
| To: | pgsql-general(at)postgresql(dot)org |
| Subject: | Re: Query to find sum of grouped counts from 2 tables |
| Date: | 2011-01-07 10:28:43 |
| Message-ID: | ig6psp$50m$1@dough.gmane.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-general |
Satish Burnwal (sburnwal) wrote on 07.01.2011 11:15:
> 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:
Hmm, I don't see why UNION shouldn't work:
SELECT type, sum(items) as count
FROM (
SELECT type, items
FROM store1
UNION ALL
SELECT type, items
FROM store2
) t
GROUP BY type
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Richard Huxton | 2011-01-07 12:40:03 | Re: Inserting data from one database to another using stored functions |
| Previous Message | Satish Burnwal (sburnwal) | 2011-01-07 10:15:25 | Query to find sum of grouped counts from 2 tables |