From: | "mlunnon (at) RWA" <mlunnon(at)rwa-net(dot)co(dot)uk> |
---|---|
To: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: conditional query? |
Date: | 2003-10-31 18:14:56 |
Message-ID: | 3FA2A6A0.3010907@rwa-net.co.uk |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
<!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(dot)LNX(dot)4(dot)44(dot)0310311544270(dot)13725-100000(at)matrix(dot)gatewaynet(dot)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>
Attachment | Content-Type | Size |
---|---|---|
unknown_filename | text/html | 2.2 KB |
From | Date | Subject | |
---|---|---|---|
Next Message | Shaun Watts | 2003-10-31 19:19:49 | selecting problems |
Previous Message | Achilleus Mantzios | 2003-10-31 13:47:49 | Re: conditional query? |