pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: José Soares <jose@sferacarta.com>
To: secret <secret@kearneydev.com>
Cc: Jackson, DeJuan <djackson@cpsgroup.com>
Cc: PG-SQL <pgsql-sql@postgresql.org>
Subject: Re: [SQL] Oddities with NULL and GROUP BY
Date: Mon, 17 May 1999 17:28:39 +0200
Message-ID: <374035A7.F51806F@sferacarta.com> (raw)
References: <D05EF808F2DFD211AE4A00105AA1B5D216ECC9@cpsmail>
	<3740164A.E604F6E5@kearneydev.com>

secret ha scritto:

> "Jackson, DeJuan" wrote:
>
> > The behavior is valid, if you define NULL as meaning undefined.
> > In other words when you define something as NULL you're saying, "I don't
> > know what it is. It could be equal or not."
> >         -DEJ
> >
> > > -----Original Message-----
> > > From: secret [SMTP:secret@kearneydev.com]
> > > Sent: Friday, May 14, 1999 11:58 AM
> > > To:   PG-SQL
> > > Subject:      [SQL] Oddities with NULL and GROUP BY
> > >
> > >     Maybe there is something I don't know about how GROUP BY should
> > > work, but if I have a table like:
> > > a,b,c
> > > 1,1,1
> > > 1,1,2
> > > 1,1,3
> > > 1,2,1
> > > 1,3,1
> > >
> > > And I say SELECT a,b,sum(c) FROm .. GROUP BY a,b I get
> > > 1,1,6
> > > 1,2,1
> > > 1,3,1
> > >
> > > So whenever a or b changes we get a new summed row, well if I have rows
> > > where a or b are null, this doesn't happen, infact I seem to get all
> > > those rows individually... Like if:
> > > 1,1,1
> > > 1,1,3
> > > 1,NULL,10
> > > 1,NULL,20
> > > 1,2,3
> > >
> > > I get:
> > > 1,1,4
> > > 1,NULL,10
> > > 1,NULL,20
> > > 1,2,3
> > >
> > > Shouldn't I get 1,NULL,30?  Ie shouldn't NULL be treated like any other
> > > value?  Or is there some bit of information I'm missing?  I can set
> > > everything from NULL to 0 if need be, but I'd rather not...
> > >
> > > David Secret
> > > MIS Director
> > > Kearney Development Co., Inc.
> > >
>
>     IBM's DB/2 Disagrees, so does Oracle8!
>
> Here is a cut & paste from Oracle SQL+:
>
> SQL> select * from z;
>
>         A         B
> --------- ---------
>         1         1
>         1         2
>                   5
>                  10
>
> SQL> select a,sum(b) from z group by a;
>
>         A    SUM(B)
> --------- ---------
>         1         3
>                  15
>
> SQL>
>
>     I'm going to report this as a bug now that I've verified 2 major database
> vendors perform the task as I would expect them to, and PostgreSQL does it
> very differently.  The question is really is NULL=NULL, which I would say it
> should be.

I tried it in PostgreSQL 6.5beta1 with the same result:

select * from z;
a| b
-+--
1| 1
1| 2
 | 5
 |10
(4 rows)

select a,sum(b) from z group by a;
a|sum
-+---
1|  3
 | 15
(2 rows)

The Pratical SQL Handbook at page 171 says:
Since nulls represent "the great unknown", there is no way to know
whether one null is equal to any other null. Each unknown value
may or may not be different from another.
However, if the grouping column contains more than one null,
all of them are put into a single group.

Thus: NULL!=NULL but on GROUP BY it is considered as NULL=NULL.

José




--
______________________________________________________________
PostgreSQL 6.5.0 on i586-pc-linux-gnu, compiled by gcc 2.7.2.3
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jose'

view thread (15+ messages)  latest in thread

Message-ID: <374035A7.F51806F@sferacarta.com>
Permalink:  ../374035A7.F51806F@sferacarta.com/
Also on:    postgresql.org/message-id/374035A7.F51806F@sferacarta.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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: jose@sferacarta.com, secret@kearneydev.com, djackson@cpsgroup.com
  Subject: Re: [SQL] Oddities with NULL and GROUP BY
  In-Reply-To: <374035A7.F51806F@sferacarta.com>
  References: <D05EF808F2DFD211AE4A00105AA1B5D216ECC9@cpsmail> <3740164A.E604F6E5@kearneydev.com> <374035A7.F51806F@sferacarta.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