Received: from bologna.nettuno.it (bologna.nettuno.it [193.43.2.1]) by hub.org (8.9.2/8.9.1) with ESMTP id DAA20662 for ; Tue, 16 Feb 1999 03:54:52 -0500 (EST) (envelope-from sferac@bo.nettuno.it) Received: from proxy.sferacarta.com (mail@sfcabop1.nettuno.it [193.207.10.213]) by bologna.nettuno.it (8.8.6/8.8.6/NETTuno 3.1) with ESMTP id JAA21374; Tue, 16 Feb 1999 09:54:37 +0100 (MET) Received: from rosso.sferacarta.com (bo.nettuno.it) [10.20.30.5] by proxy.sferacarta.com with esmtp (Exim 2.05 #1 (Debian)) id 10ChCQ-0001O3-00; Tue, 16 Feb 1999 09:53:50 +0000 Message-ID: <36C931A3.3941DCAE@bo.nettuno.it> Date: Tue, 16 Feb 1999 09:51:47 +0100 From: "jose' soares" Organization: Sferacarta Software X-Mailer: Mozilla 4.5 [it] (Win95; I) X-Accept-Language: it MIME-Version: 1.0 To: Paul Ramsey CC: pgsql-sql@postgresql.org Subject: Re: [SQL] Booleans and Casting References: <36C49E14.84C93DA5@refractions.net> Content-Type: multipart/alternative; boundary="------------0A783723EB9E3BF9001E0493" --------------0A783723EB9E3BF9001E0493 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Paul Ramsey ha scritto: > I have the following SQL statement, which doesn't work (though I wish it > did): > > select sum(value * ( plulabel = 'FO' )) from btmm group by gis_tag; > > PG quite correctly complains that: > > ERROR: There is more than one possible operator '*' for types > 'float4' and 'bool' You will have to retype this query using an explicit > cast > > However, when I do this: > > select sum(value * int( plulabel = 'FO' )) from btmm group by gis_tag; > > I still get an error: > > ERROR: No such function 'int' with the specified attributes > > I can find no function at all to use to cast booleans to what I want, > which is 1 = true, 0 = false (IE, the standard cast for boolean -> > number). > > Any advice? > > -- > __ > / > | Paul Ramsey > | Refractions Research > | Email: pramsey@refractions.net > | Phone: (250) 885-0632 > \_ create function int(bool) returns int4 as 'begin if $1 then RETURN 1; else RETURN 0; end if; end;' language 'plpgsql'; CREATE drop table btmm; DROP create table btmm (gis_tga text, value float, plulabel text); CREATE insert into btmm values('a',13.2,'FO'); INSERT 190188 1 insert into btmm values('b',1.32,'FO'); INSERT 190189 1 insert into btmm values('c',31.32,'RO'); INSERT 190190 1 select sum(value * int( plulabel = 'FO' )) from btmm group by gis_tga; sum ---- 13.2 1.32 0 (3 rows) -- - Jose' - And behold, I tell you these things that ye may learn wisdom; that ye may learn that when ye are in the service of your fellow beings ye are only in the service of your God. - Mosiah 2:17 - --------------0A783723EB9E3BF9001E0493 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit  

Paul Ramsey ha scritto:

I have the following SQL statement, which doesn't work (though I wish it
did):

  select sum(value * ( plulabel = 'FO' )) from btmm group by gis_tag;

PG quite correctly complains that:

  ERROR:  There is more than one possible operator '*' for types
'float4' and 'bool' You will have to retype this query using an explicit
cast

However, when I do this:

  select sum(value * int( plulabel = 'FO' )) from btmm group by gis_tag;

I still get an error:

  ERROR:  No such function 'int' with the specified attributes

I can find no function at all to use to cast booleans to what I want,
which is 1 = true, 0 = false (IE, the standard cast for boolean ->
number).

Any advice?

--
      __
     /
     | Paul Ramsey
     | Refractions Research
     | Email: pramsey@refractions.net
     | Phone: (250) 885-0632
     \_

create function int(bool) returns int4 as
'begin
        if $1 then
            RETURN 1;
        else
            RETURN 0;
        end if;
end;' language 'plpgsql';
CREATE

drop table btmm;
DROP
create table btmm (gis_tga text, value float, plulabel text);
CREATE
insert into btmm values('a',13.2,'FO');
INSERT 190188 1
insert into btmm values('b',1.32,'FO');
INSERT 190189 1
insert into btmm values('c',31.32,'RO');
INSERT 190190 1
select sum(value * int( plulabel = 'FO' )) from btmm group by gis_tga;
 sum
----
13.2
1.32
   0
(3 rows)
 

--
                               - Jose' -

And behold, I tell you these things that ye may learn wisdom; that ye may
learn that when ye are in the service of your fellow beings ye are only
in the service of your God.                - Mosiah 2:17 -
  --------------0A783723EB9E3BF9001E0493--