Re: RE: counting distinct rows on more than one column

From: "Jim Ballard" <jballard(at)netezza(dot)com>
To: "Jeff Eckermann" <jeckermann(at)verio(dot)net>, "'Dirk Lutzebaeck'" <lutzeb(at)aeccom(dot)com>, "Michael Fork" <mfork(at)toledolink(dot)com>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: RE: counting distinct rows on more than one column
Date: 2001-03-28 22:27:09
Message-ID: 01b001c0b7d6$3fccbac0$8300a8c0@planet
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

If the fields are fixed length character type, then the simpler
concatenation should work.

Actually, the requirement is only that all but the final field be fixed
length.

And if they aren't fixed length, you can cast them to be such, as long as
you know the maximum length of the string values, as in the following where
that maximum is 20 and we are looking at 3 fields:

select count(distinct (cast(field1 as char(20)) || cast(field2 as char(20))
|| field3)) from ...

Jim Ballard

----- Original Message -----
From: "Jeff Eckermann" <jeckermann(at)verio(dot)net>
To: "'Dirk Lutzebaeck'" <lutzeb(at)aeccom(dot)com>; "Michael Fork"
<mfork(at)toledolink(dot)com>
Cc: <pgsql-sql(at)postgresql(dot)org>
Sent: Wednesday, March 28, 2001 3:43 PM
Subject: [SQL] RE: counting distinct rows on more than one column

> I don't think this will necessarily work:
>
> field1 | field2
> aa | ab
> a | aab
>
> These are two distinct rows, so should be counted as two.
> The proposed method would count them as one.
> You can get around this problem by doing:
> count (distinct (a || x || b))
> where x is some character not found in your data.
>
> > -----Original Message-----
> > From: Dirk Lutzebaeck [SMTP:lutzeb(at)aeccom(dot)com]
> > Sent: Wednesday, March 28, 2001 1:32 PM
> > To: Michael Fork
> > Cc: pgsql-sql(at)postgresql(dot)org
> > Subject: Re: counting distinct rows on more than one column
> >
> > Michael Fork writes:
> > > In 7.0.3, I believe the following would work:
> > >
> > > SELECT count(distinct(a || b)) FROM t;
> >
> > Great, this works! I don't quite get it why...
> >
> > Dirk
> >
> > ---------------------------(end of broadcast)---------------------------
> > TIP 3: if posting/reading through Usenet, please send an appropriate
> > subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> > message can get through to the mailing list cleanly
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo(at)postgresql(dot)org
>

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Marcos Minshew 2001-03-28 22:53:30 SELECT ... FOR UPDATE
Previous Message Jeff Eckermann 2001-03-28 20:43:26 RE: counting distinct rows on more than one column