pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: hubert depesz lubaczewski <depesz@depesz.com>
To: Kiran <kumar.m.kiran@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Best Fit SQL query statement
Date: Fri, 10 Aug 2007 09:59:49 +0200
Message-ID: <20070810075949.GA31829@depesz.com> (raw)
In-Reply-To: <1186390627.045596.148460@j4g2000prf.googlegroups.com>
References: <1186390627.045596.148460@j4g2000prf.googlegroups.com>

On Mon, Aug 06, 2007 at 01:57:07AM -0700, Kiran wrote:
> Could anyone  help me in writing Best Fit SQL statement.
> Suppose we have table t1 with coloumn t1 (text) with following rows.
> 98456
> 98457
> 9845
> 9846
> 984
> 985
> 98
> 99
> and if I query on 98456 the result must be 98456,
> However if I query on 98455 the result must be 9845
> and If I query 9849 the result must be 984

select t1.t1 from t1 where '98456' like t1.t1||'%' order by length(t1.t1) desc limit 1;

should be ok.

depesz

-- 
quicksil1er: "postgres is excellent, but like any DB it requires a
highly paid DBA.  here's my CV!" :)
http://www.depesz.com/ - blog dla ciebie (i moje CV)



view thread (8+ messages)  latest in thread

Message-ID: <20070810075949.GA31829@depesz.com>
Permalink:  ../20070810075949.GA31829@depesz.com/
Also on:    postgresql.org/message-id/20070810075949.GA31829@depesz.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: depesz@depesz.com, kumar.m.kiran@gmail.com
  Subject: Re: Best Fit SQL query statement
  In-Reply-To: <20070810075949.GA31829@depesz.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox