From: mlunnon @ RWA <mlunnon@rwa-net.co.uk>
To: pgsql-sql@postgresql.org
Subject: Re: conditional query?
Date: Fri, 31 Oct 2003 18:14:56 +0000
Message-ID: <3FA2A6A0.3010907@rwa-net.co.uk> (raw)
In-Reply-To: <Pine.LNX.4.44.0310311544270.13725-100000@matrix.gatewaynet.com>
References: <Pine.LNX.4.44.0310311544270.13725-100000@matrix.gatewaynet.com>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<title></title>
</head>
<body text="#000000" bgcolor="#ffffff">
Try something like<br>
<br>
SELECT p2.* <br>
FROM profile p1, profile p2 <br>
WHERE ( p1.id =1 AND useParenAddres = 'N' AND p2.id = p1.id ) <br>
OR ( p1.id =1 AND useParenAddres = 'Y' AND p2.id = p1.parentId) <br>
<br>
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.<br>
<br>
Another way to solve the grand parent thing would be to define a
recursive function.<br>
<br>
Happy coding.<br>
<br>
Cheers<br>
Matthew<br>
<br>
<br>
Achilleus Mantzios wrote:<br>
<blockquote type="cite"
cite="midPine.LNX.4.44.0310311544270.13725-100000@matrix.gatewaynet.com">
<pre wrap="">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 :
</pre>
<blockquote type="cite">
<pre wrap="">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
</pre>
</blockquote>
<pre wrap=""><!---->
</pre>
</blockquote>
</body>
</html>
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: mlunnon@rwa-net.co.uk
Subject: Re: conditional query?
In-Reply-To: <3FA2A6A0.3010907@rwa-net.co.uk>
* 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