X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 18C9CD1B56D for ; Fri, 31 Oct 2003 18:15:36 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 47555-02 for ; Fri, 31 Oct 2003 14:15:05 -0400 (AST) Received: from mta07-svc.ntlworld.com (mta07-svc.ntlworld.com [62.253.162.47]) by svr1.postgresql.org (Postfix) with ESMTP id 6253BD1B4E6 for ; Fri, 31 Oct 2003 14:15:03 -0400 (AST) Received: from rwa-net.co.uk ([80.5.126.158]) by mta07-svc.ntlworld.com (InterMail vM.4.01.03.37 201-229-121-137-20020806) with ESMTP id <20031031181458.YCJE2637.mta07-svc.ntlworld.com@rwa-net.co.uk> for ; Fri, 31 Oct 2003 18:14:58 +0000 Message-ID: <3FA2A6A0.3010907@rwa-net.co.uk> Date: Fri, 31 Oct 2003 18:14:56 +0000 From: "mlunnon @ RWA" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: conditional query? References: In-Reply-To: Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/413 X-Sequence-Number: 15813 Try something like

SELECT p2.*
FROM profile p1, profile p2
WHERE ( p1.id =1 AND useParenAddres = 'N' AND p2.id = p1.id )
OR ( p1.id =1 AND useParenAddres = 'Y' AND p2.id = p1.parentId)

Obviously this won't work if you have more than one level of parent hood, i.e. it would pick up a grand parent.  If this is the case then there is some kind of tree walking functionality in Postgres but I don't know how portable this is or whether it will solve your problem.

Another way to solve the grand parent thing would be to define a recursive function.

Happy coding.

Cheers
Matthew


Achilleus Mantzios wrote:
Why dont you try a combination of
CASE WHEN ... THEN ... ELSE ... END construct
along with a LEFT OUTER join (in case parentId is null).

Not sure how "portable" the above will be.

O kyrios Frank Morton egrapse stis Oct 31, 2003 :

  
I have a table called a "profile" that has company addresses as well
as individual contact information. Simpifying:

id (the primary key)
parentId (any profile can have a parent ie, contact parent is a company)
address (for this example, will just have one piece of the address)
useParentAddress

If "useParentAddress"=="Y", that means that the parent address of this
person should really be used for mailings. If == "N" then the address
with that profile is the right one to use.

Is there any way to do a single select to get a single "address" back
that is the right one depending on the value of "useParentAddress"
field?

Also want to make this sql as portable as possible.

Will appreciate any ideas.


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