X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (mx2.hub.org [200.46.204.254]) by postgresql.org (Postfix) with ESMTP id C570E11F675B for ; Sat, 15 Apr 2006 06:56:15 -0300 (ADT) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Score: 0.758 X-Spam-Level: X-Spam-Status: No, score=0.758 required=5 tests=[BAYES_00=-2.599, FROM_LOCAL_NOVOWEL=2.861, HTML_40_50=0.496, HTML_MESSAGE=0.001, SPF_PASS=-0.001] Received: from postgresql.org ([200.46.204.71]) by localhost (mx2.hub.org [200.46.204.254]) (amavisd-new, port 10024) with ESMTP id F8pBhVI2Unsr for ; Sat, 15 Apr 2006 06:56:09 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from nz-out-0102.google.com (nz-out-0102.google.com [64.233.162.198]) by postgresql.org (Postfix) with ESMTP id 17AD411F6752 for ; Sat, 15 Apr 2006 06:56:07 -0300 (ADT) Received: by nz-out-0102.google.com with SMTP id m7so196688nzf for ; Sat, 15 Apr 2006 02:56:08 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=CrN3TwiZ/NY0kxP++Bgqw/4UfjzqG0kkL2MddZ4PY+cTMaCoijNpTCP3kxb1Gopxm5640efUlydi/9vebZVLLJ5QJGEGd/MSTPNFbTfFReYCbVT0Rh1Kaxb17GFADwfzpJbx9rW8rqpWStwFxZwbaOKHN9yO+XyND7vhzJi12sc= Received: by 10.36.80.11 with SMTP id d11mr2775372nzb; Sat, 15 Apr 2006 02:56:08 -0700 (PDT) Received: by 10.36.158.13 with HTTP; Sat, 15 Apr 2006 02:56:08 -0700 (PDT) Message-ID: Date: Sat, 15 Apr 2006 13:56:08 +0400 From: Luckys To: marchesini@unipg.it Subject: Re: how to solve this problem Cc: "Steve Crawford" , pgsql-sql@postgresql.org In-Reply-To: <1145029254.4738.9.camel@geologia5.agora.ing.unipg.it> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_7138_13033068.1145094968141" References: <1144934290.5079.32.camel@geologia5.agora.ing.unipg.it> <443E99C5.3080800@pinpointresearch.com> <1145029254.4738.9.camel@geologia5.agora.ing.unipg.it> X-Archive-Number: 200604/100 X-Sequence-Number: 24909 ------=_Part_7138_13033068.1145094968141 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline the same can be even achieved, instead of having 20 different columns, in a single column varchar(20) vincs('11010111011101110010'). Luckys. On 4/14/06, ivan marchesini wrote: > > Thank you very much!!! > your suggestion seems really usefull!! I will try it very soon!!! > ivan > > > Il giorno gio, 13/04/2006 alle 11.34 -0700, Steve Crawford ha scritto: > > > I have a table where there are 20 columns named > > > vinc1, vinc2, vinc3, vinc4, etc.... > > > > > > the values contained into each column are simply 1 or 0 (each column > is > > > dichotomic) > > > 1 means presence > > > 0 means absence > > > > > > I would obtain a column (new_column) containg the name of the columns= , > > > comma separated, where the value is =3D 1 > > > > > > for example: > > > > > > vinc1 vinc2 vinc3 vinc4 new_column > > > 1 0 1 0 vinc1,vinc3 > > > 0 0 0 1 vinc4 > > > 0 1 1 1 vinc2,vinc3,vinc4 > > > > > > can someone help me to find the best way to obtain this result??? > > > thank you very much > > > > Here's a brute-force method. Maybe someone else has a more elegant way. > > More info on the nature of your data and what you are trying to obtain > > from it would help in finding such elegance. > > > > select > > substr( > > case when vinc1 =3D 1 then ',vinc1' else '' || > > case when vinc2 =3D 1 then ',vinc2' else '' || > > ... > > case when vinc20 =3D 1 then ',vinc20' else '' > > ,2) as new_column ... > > > > As to the binary representation mentioned elsewhere, the idea is that > > you can view vinc1-vinc20 as bits in a binary number equal to vinc1 + > > 2*vinc2 + 4*vinc3 + 8*vinc4...2^19*vinc20. Whether or not this is usefu= l > > depends on what you are trying to do. > > > > Cheers, > > Steve > > > -- > Ivan Marchesini > Department of Civil and Environmental Engineering > University of Perugia > Via G. Duranti 93/a > 06125 > Perugia (Italy) > e-mail: marchesini@unipg.it > ivan.marchesini@gmail.com > tel: +39(0)755853760 > fax: +39(0)755853756 > jabber: geoivan73@jabber.org > > > > > ---------------------------(end of broadcast)--------------------------- > TIP 5: don't forget to increase your free space map settings > ------=_Part_7138_13033068.1145094968141 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
the same can be even achieved, instead of having 20 different columns,=
in a single column varchar(20) vincs('11010111011101110010').
 
Luckys.
 
On 4/14/06, = ivan marchesini <marchesini@u= nipg.it> wrote:
Thank you very much!!!
your s= uggestion seems really usefull!! I will try it very soon!!!
ivan


Il giorno gio, 13/04/2006 alle 11.34 -0700, Steve Crawford ha scritto:<= br>> > I have a table where there are 20 columns named
> > v= inc1, vinc2, vinc3, vinc4, etc....
> >
> > the values con= tained into each column are simply 1 or 0  (each column is
> > dichotomic)
> > 1 means presence
> > 0 mean= s absence
> >
> > I would obtain a column (new_column) co= ntaing the name of the columns,
> > comma separated, where the val= ue is =3D 1
> >
> > for example:
> >
> > vinc1&nbs= p;      vinc2   vinc3   vinc4&= nbsp;  new_column
> > 1   0    =    1       0    = ;   vinc1,vinc3
> > 0   0   &nb= sp;   0       1   &n= bsp;   vinc4
> > 0   1    =    1       1    = ;   vinc2,vinc3,vinc4
> >
> > can someone help me to find the best way to obta= in this result???
> > thank you very much
>
> Here's a= brute-force method. Maybe someone else has a more elegant way.
> Mor= e info on the nature of your data and what you are trying to obtain
> from it would help in finding such elegance.
>
> selec= t
>    substr(
>    cas= e when vinc1 =3D 1 then ',vinc1' else '' ||
>    = case when vinc2 =3D 1 then ',vinc2' else '' ||
>   &nb= sp;...
>    case when vinc20 =3D 1 then ',vinc20' else = ''
>    ,2) as new_column ...
>
> As = to the binary representation mentioned elsewhere, the idea is that
> = you can view vinc1-vinc20 as bits in a binary number equal to vinc1 +
> 2*vinc2 + 4*vinc3 + 8*vinc4...2^19*vinc20. Whether or not this is = useful
> depends on what you are trying to do.
>
> Cheers= ,
> Steve
>
--
Ivan Marchesini
Department of Civil and= Environmental Engineering
University of Perugia
Via G. Duranti 93/a
06125
Perugia (Italy= )
e-mail: marchesini@unipg.it=
       ivan.marchesini@gmail.com
tel: +39(0)755853760
fax: +39(0)755853756
jabber: geoivan73@jabber.org




--= -------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

------=_Part_7138_13033068.1145094968141--