pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedconditional query?
4+ messages / 4 participants
[nested] [flat]
* conditional query?
@ 2003-10-31 13:30 Frank Morton <fmorton@base2inc.com>
0 siblings, 1 reply; 4+ messages in thread
From: Frank Morton @ 2003-10-31 13:30 UTC (permalink / raw)
To: pgsql-sql
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.
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: conditional query?
@ 2003-10-31 13:47 Achilleus Mantzios <achill@matrix.gatewaynet.com>
parent: Frank Morton <fmorton@base2inc.com>
0 siblings, 1 reply; 4+ messages in thread
From: Achilleus Mantzios @ 2003-10-31 13:47 UTC (permalink / raw)
To: Frank Morton <fmorton@base2inc.com>; +Cc: pgsql-sql
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
>
--
-Achilleus
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: conditional query?
@ 2003-10-31 18:14 mlunnon @ RWA <mlunnon@rwa-net.co.uk>
parent: Achilleus Mantzios <achill@matrix.gatewaynet.com>
0 siblings, 0 replies; 4+ messages in thread
From: mlunnon @ RWA @ 2003-10-31 18:14 UTC (permalink / raw)
To: 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.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>
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: conditional query?
@ 2003-11-02 04:04 zen31329@zen.co.uk
0 siblings, 0 replies; 4+ messages in thread
From: zen31329@zen.co.uk @ 2003-11-02 04:04 UTC (permalink / raw)
To: pgsql-sql
As you say, it is easiest if there is only one level of 'parenting'..!
SELECT p.id,
CASE WHEN p.UseParentAddress = 'Y' THEN
(SELECT address FROM profile WHERE id = p.parentid)
ELSE address AS address
FROM Profile p
If you do have multiple levels, you'll need to recurse, either with a cursor or a while loop - it might make sense to build a temporary table of all profiles for which you need addresses, and then loop around it until they are all filled in (or similar).
Cheers,
Matt
Mlunnon @ RWA <mlunnon@rwa-net.co.uk> wrote :
> 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
--Matt
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2003-11-02 04:04 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-10-31 13:30 conditional query? Frank Morton <fmorton@base2inc.com>
2003-10-31 13:47 ` Achilleus Mantzios <achill@matrix.gatewaynet.com>
2003-10-31 18:14 ` mlunnon @ RWA <mlunnon@rwa-net.co.uk>
2003-11-02 04:04 Re: conditional query? zen31329@zen.co.uk
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