GROUP BY on a column which might exist in one of two tables - Mailing list pgsql-sql

From Mark Stosberg
Subject GROUP BY on a column which might exist in one of two tables
Date
Msg-id 1214401817.30599.40.camel@localhost
Whole thread Raw
Responses Re: GROUP BY on a column which might exist in one of two tables  (hubert depesz lubaczewski <depesz@depesz.com>)
Re: GROUP BY on a column which might exist in one of two tables  ("Greg Sabino Mullane" <greg@turnstep.com>)
List pgsql-sql
Hello,

I could use some help figuring out a complex "GROUP BY".
As a setup, let's say I have two tables which stores a single "hit" or
"view" in a row

hits hit_id partner_id

views view_id partner_id

There is of course a "partners" table with a "partner_id" column. 

My target result is more like

partner_id
total_views
total_hits

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. 

My first attempt at the SQL for this GROUP
COALESCE(hits.partner_id,views.partner_id) which didn't work.

I'm stuck on what to try next. 

Thanks!
    Mark






pgsql-sql by date:

Previous
From: Allan Kamau
Date:
Subject: Re: Sequential event query
Next
From: hubert depesz lubaczewski
Date:
Subject: Re: GROUP BY on a column which might exist in one of two tables