Re: how to solve this problem

From: Luckys <plpgsql(at)gmail(dot)com>
To: marchesini(at)unipg(dot)it
Cc: "Steve Crawford" <scrawford(at)pinpointresearch(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: how to solve this problem
Date: 2006-04-15 09:56:08
Message-ID: b80d582c0604150256w3bd3314bh8470edca1767b87d@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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(at)unipg(dot)it> 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 = 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 = 1 then ',vinc1' else '' ||
> > case when vinc2 = 1 then ',vinc2' else '' ||
> > ...
> > case when vinc20 = 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(at)unipg(dot)it
> ivan(dot)marchesini(at)gmail(dot)com
> tel: +39(0)755853760
> fax: +39(0)755853756
> jabber: geoivan73(at)jabber(dot)org
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings
>

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Bruno Wolff III 2006-04-17 19:59:25 Re: Please help with a query..
Previous Message ivan marchesini 2006-04-14 15:40:53 Re: how to solve this problem