query

Lists: pgsql-sql
From: "superboy143 (sent by Nabble(dot)com)" <lists(at)nabble(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: query
Date: 2006-02-07 09:45:50
Message-ID: 2798161.post@talk.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql


Hello,

I have a table in which I have a field with format like 100101. It has many values like 100101, 100102, 100103, 100201, 100202, 100301. I have to write a query such that I have to get only distinct values such that they contain only the substring I need. If I give 10 as substring, then it should return only 100101 or 100102 but not both i.e if the last two characters are not same it should not return both of them. It should return only values starting with 10 the middle two values should be distinct and the last two characters may be anything.
--
View this message in context: http://www.nabble.com/query-t1074932.html#a2798161
Sent from the PostgreSQL - sql forum at Nabble.com.


From: Richard Huxton <dev(at)archonet(dot)com>
To: superboy143 <superboy143(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: query
Date: 2006-02-07 10:34:59
Message-ID: 43E877D3.9090509@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

superboy143 (sent by Nabble.com) wrote:
> Hello,
>
> I have a table in which I have a field with format like 100101. It
> has many values like 100101, 100102, 100103, 100201, 100202, 100301.

OK - so they look like numbers but aren't.

> I have to write a query such that I have to get only distinct values
> such that they contain only the substring I need. If I give 10 as
> substring, then it should return only 100101 or 100102 but not both
> i.e if the last two characters are not same it should not return both
> of them.

The statement of the rule and the example contradict each other. Surely
100101 and 100102 are distinct values. Surely they contain "10" as a
substring. So according to your rule the should be returned.

> It should return only values starting with 10 the middle two
> values should be distinct and the last two characters may be
> anything.

This seems to agree with your example and not your rule.

So:
1. You don't want distinct values of your field, you want distinct
substrings of your field?
2. You don't want to match a substring, you want to match the start of
the string?
3. You want only one of 100101 or 100102 but you don't care which?

Is this correct?

By the way - it looks to me like you are trying to store multiple values
in one column. If you split the values into their own columns I'd guess
your query would be much easier.

--
Richard Huxton
Archonet Ltd


From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: superboy143 <superboy143(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: query
Date: 2006-02-10 06:11:29
Message-ID: 20060210061129.GA26002@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

On Tue, Feb 07, 2006 at 01:45:50 -0800,
"superboy143 (sent by Nabble.com)" <lists(at)nabble(dot)com> wrote:
>
> I have a table in which I have a field with format like 100101. It has many values like 100101, 100102, 100103, 100201, 100202, 100301. I have to write a query such that I have to get only distinct values such that they contain only the substring I need. If I give 10 as substring, then it should return only 100101 or 100102 but not both i.e if the last two characters are not same it should not return both of them. It should return only values starting with 10 the middle two values should be distinct and the last two characters may be anything.

You can probably use the Postgres extension DISTINCT ON to do what you want.


From: Ken Hill <ken(at)scottshill(dot)com>
To: Bruno Wolff III <bruno(at)wolff(dot)to>
Cc: superboy143 <superboy143(at)gmail(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: query
Date: 2006-02-10 16:22:03
Message-ID: 1139588523.29808.10.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

On Fri, 2006-02-10 at 00:11 -0600, Bruno Wolff III wrote:

> On Tue, Feb 07, 2006 at 01:45:50 -0800,
> "superboy143 (sent by Nabble.com)" <lists(at)nabble(dot)com> wrote:
> >
> > I have a table in which I have a field with format like 100101. It has many values like 100101, 100102, 100103, 100201, 100202, 100301. I have to write a query such that I have to get only distinct values such that they contain only the substring I need. If I give 10 as substring, then it should return only 100101 or 100102 but not both i.e if the last two characters are not same it should not return both of them. It should return only values starting with 10 the middle two values should be distinct and the last two characters may be anything.
>
> You can probably use the Postgres extension DISTINCT ON to do what you want.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faq

Try substring(column,1,4). That should return values of 1001, 1002, 1003
when grouped.