From: Richard Huxton <dev@archonet.com>
To: Gary Stainburn <gary.stainburn@ringways.co.uk>
To: Tomasz Myrta <jasiek@klaster.net>
Cc: pgsql-sql@postgresql.org
Subject: Re: efficient count/join query
Date: Fri, 7 Feb 2003 13:36:40 +0000
Message-ID: <200302071336.40600.dev@archonet.com> (raw)
In-Reply-To: <200302071257.48028.gary.stainburn@ringways.co.uk>
References: <200302071007.28697.gary.stainburn@ringways.co.uk>
<200302071209.55330.gary.stainburn@ringways.co.uk>
<200302071257.48028.gary.stainburn@ringways.co.uk>
On Friday 07 Feb 2003 12:57 pm, Gary Stainburn wrote:
>
> Thinking about it, I'm not wanting to perform a join as such, but a merge
> of the two selects below, then some form of group by to sum() the two
> counts.
>
> select rosid as sid, rojid as jid, count(*) as count
> from roster group by sid, jid order by sid, jid;
> select hsid as sid, hjid as jid, hcount as count
> from history order by sid, jid;
>
> so that
>
> 1 2 1
> 1 3 2
>
> and
>
> 1 3 1
> 1 4 2
>
> becomes
>
> 1 2 1
> 1 3 3
> 1 4 2
You want a UNION, something like (untested):
CREATE VIEW all_counts AS
SELECT rosid as sid, rojid as jid, count(*) as all_count FROM roster GROUP BY
sid,jid
UNION ALL
SELECT hsid,hjid,hcount FROM history;
SELECT sid,jid,SUM(all_count) FROM all_counts GROUP BY sid,jid;
--
Richard Huxton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: dev@archonet.com, gary.stainburn@ringways.co.uk, jasiek@klaster.net
Subject: Re: efficient count/join query
In-Reply-To: <200302071336.40600.dev@archonet.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox