Re: GROUP BY on a column which might exist in one of two tables

From: "Greg Sabino Mullane" <greg(at)turnstep(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: GROUP BY on a column which might exist in one of two tables
Date: 2008-06-25 14:20:53
Message-ID: b65a360be1225154e022b431725b9351@biglumber.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

> Where the totals are "counts" of the the rows in the hits and views
> tables. There should be no rows for partners without hits or views.

How about something like this?:

SELECT partner_id, COUNT(view_id) AS total_views, COUNT(hit_id) AS total_hits
FROM (
SELECT partner_id, hit_id, NULL AS view_id FROM hits
UNION ALL
SELECT partner_id, NULL, view_id FROM views
) AS foo
GROUP BY 1;

- --
Greg Sabino Mullane greg(at)turnstep(dot)com
End Point Corporation
PGP Key: 0x14964AC8 200806251019
http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8
-----BEGIN PGP SIGNATURE-----

iEYEAREDAAYFAkhiU+8ACgkQvJuQZxSWSsgNiACgmrUWfTv1ZSiiexOKja64p1F8
1hYAn3i+tYoEOIs2NIcSrExlvoyfJE+X
=ryrm
-----END PGP SIGNATURE-----

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Steve Crawford 2008-06-25 17:03:39 Re: Sequential event query
Previous Message Mark Stosberg 2008-06-25 14:17:55 Re: GROUP BY on a column which might exist in one of two tables