pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Aaron Bono <postgresql@aranya.com>
To: Postgres SQL language list <pgsql-sql@postgresql.org>
Subject: Unions and Grouping
Date: Fri, 15 Dec 2006 15:15:12 -0600
Message-ID: <bf05e51c0612151315s59e6647etc2dd45e940d41a2a@mail.gmail.com> (raw)

I have a question about the SQL Specifications in regards to Unions...

I recently put together a query that involved unions similar to the
following:

SELECT
    'Query 1' as id,
    my_value
FROM my_view
UNION
SELECT
    'Query 2' as id,
    my_value
FROM my_other_view
;

The first query in the union gave me 39 records and the second gave me 34
records.  I was expecting the union to give me 39 + 34 = 73 records.

When I ran this against DB2, I got 35 records (not sure about PostgreSQL -
will have to try it when I get home).  What I found was when I did a group
by my_value on each query I got two values that then added to 35.  The
reason was, my_value was duplicated in my_view and in my_other_view.  What
the Union appeared to be doing was to gather the data and then do a group by
on the complete results.  I expected it to only eliminate duplicates BETWEEN
the two queries, not WITHIN the queries.

My question, what do the SQL Specifications say should happen on a Union?
Is it supposed to eliminate duplicates even WITHIN the individual queries
that are being unioned?

Thanks!

-- 
==================================================================
   Aaron Bono
   Aranya Software Technologies, Inc.
   http://www.aranya.com
   http://codeelixir.com
==================================================================

view thread (5+ messages)  latest in thread

Message-ID: <bf05e51c0612151315s59e6647etc2dd45e940d41a2a@mail.gmail.com>
Permalink:  ../bf05e51c0612151315s59e6647etc2dd45e940d41a2a@mail.gmail.com/
Also on:    postgresql.org/message-id/bf05e51c0612151315s59e6647etc2dd45e940d41a2a@mail.gmail.com

 · 

reply

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: postgresql@aranya.com
  Subject: Re: Unions and Grouping
  In-Reply-To: <bf05e51c0612151315s59e6647etc2dd45e940d41a2a@mail.gmail.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