From: Shane Ambler <pgsql@007Marketing.com>
To: M.P.Dankoor <m.p.dankoor@gmail.com>
Cc: devil live <definite_ocean@hotmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Conditional SQL Query
Date: Fri, 12 Jan 2007 10:05:25 +1030
Message-ID: <45A6C9BD.3020002@007Marketing.com> (raw)
In-Reply-To: <45A6B3C6.7000707@gmail.com>
References: <BAY20-F159C48AE0E201B150535BFEEB10@phx.gbl>
<45A6B3C6.7000707@gmail.com>
M.P.Dankoor wrote:
> devil live wrote:
>> how can I write a query to get right ingredients of a product basis on
>> production_no field
>>
>> such as;
>>
>> first check production_no if product_tree_special table if not found
>> then look at template table...
>>
>> What are your suggestions?
>>
I think M.P. Dankoor's suggestion is close but I believe the conditional
part you are looking for would make it into this -
select PRD.product_code
,PRD.product_name
,NULL::int AS production_no
,PTT.stock_code_to_make_product
,PTT.amount
from product PRD
,product_tree_template PTT
where PRD.product_code='searchcode'
AND PRD.product_code = case
when (select production_no from product_tree_special ts
where ts.product_code=PRD.product_code) is null
then PTT.product_code else '' end
UNION
select PRD.product_code
,PRD.product_name
,PTS.production_no
,PTS.stock_code_to_make_product
,PTS.amount
from product PRD
,product_tree_special PTS
where PRD.product_code='searchcode'
AND PRD.product_code = case
when (select production_no from product_tree_special ts
where ts.product_code=PRD.product_code) is not null
then PTS.product_code else '' end
ORDER BY 1,2,4
So if we entered the following -
INSERT INTO product VALUES ('one','test one');
INSERT INTO product VALUES ('two','test two');
INSERT INTO product_tree_special VALUES (1,'one','special list',1.1);
INSERT INTO product_tree_template VALUES ('two','template parts',2.2);
change both WHERE clauses to PRD.product_code='two' you will get -
two test two <null> template parts 2.2
then change both WHERE clauses to PRD.product_code='one' you will get -
one test one 1 special list 1.1
--
Shane Ambler
pgSQL@007Marketing.com
Get Sheeky @ http://Sheeky.Biz
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: pgsql@007Marketing.com, m.p.dankoor@gmail.com, definite_ocean@hotmail.com
Subject: Re: Conditional SQL Query
In-Reply-To: <45A6C9BD.3020002@007Marketing.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