pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: Patrick Hatcher <PHatcher@macys.com>
To: pgsql-sql@postgresql.org
Subject: Re: Is it possible to use a field from another table as part of a query?
Date: Fri, 19 Jul 2002 11:54:27 -0700
Message-ID: <200207191154.27095.josh@agliodbs.com> (raw)
In-Reply-To: <OF70E61741.D785C8E7-ON88256BFB.00664267-88256BFB.00664F48@fds.com>
References: <OF70E61741.D785C8E7-ON88256BFB.00664267-88256BFB.00664F48@fds.com>


Partick,

> I have a table that contains a VARCHAR field with data formatted as such:
> 12,44,13,225
> what I would like to do is use this field in a query to another table such
> as:

1. Since you are a smart guy, I'm assuming that this "delimited" VARCHAR data 
is legacy data that you don't have a choice about re-structuring.  Because, 
of course, the normalized way to store the data would be in a subtable, not a 
VARCHAR field.

> 
> CREATE TABLE category_tree (
>   tree varchar(200)
> ) WITH OIDS;
> 
> Select * from mdc_products
> where keyf_category_home IN  (select tree from category_tree)
> 
> However, my keyf_category_home field is an INT4.  Is there a way to parse
> out the tree field so that I can define it as INT4?

Well, this is easiest thing to do:

Select * from mdc_products
 where EXISTS 
	(select tree from category_tree
		WHERE tree ~ ('(^|,)' || keyf_category_home || '(,|$)'));

(somebody please correct my regexp if I've made an error)

... but that's impossible to index.   If the table category_tree doesn't 
change often, I'd write a program to parse the data and build a normalized 
subtable containg a vertical colum of tree values.

-- 
-Josh Berkus
 Aglio Database Solutions
 San Francisco




view thread (3+ messages)  latest in thread

Message-ID: <200207191154.27095.josh@agliodbs.com>
Permalink:  ../200207191154.27095.josh@agliodbs.com/
Also on:    postgresql.org/message-id/200207191154.27095.josh@agliodbs.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: josh@agliodbs.com, PHatcher@macys.com
  Subject: Re: Is it possible to use a field from another table as part of a query?
  In-Reply-To: <200207191154.27095.josh@agliodbs.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