pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: operationsengineer1@yahoo.com
To: pgsql-sql@postgresql.org <pgsql-sql@postgresql.org>
Subject: Using Query Result in WHERE Clause
Date: Fri, 2 Jun 2006 13:06:40 -0700 (PDT)
Message-ID: <20060602200640.91950.qmail@web33304.mail.mud.yahoo.com> (raw)

hi all,

SUMMARY:

i want to replace

AND t_inspect.inspect_pass = 'f'

with a complex query that yields the same result.  the
complex query is in the form of...

SELECT DISTINCT ON (t_inspect.inspect_id)
t_inspect_result.inspect_result_pass, 
       t_inspect_result.inspect_result_id
  FROM t_inspect_result, t_inspect, t_serial_number,
t_product, t_job_number, 
       t_link_contract_number_job_number,
t_inspect_area
  WHERE -- lots of noise here
  ORDER BY t_inspect.inspect_id,
inspect_result_timestamp DESC

can i do this?  if so, how?

DETAILED EXAMPLE:

i have the following query (please focus on the *key*
element, the rest of the query is just noise):

SELECT t_product.product_id, t_product.product_number,
       t_serial_number.serial_number_id,
t_serial_number.serial_number,
       t_inspect_result.inspect_result_id,
t_inspect_area.inspect_area || ', ' ||
t_inspect_result.inspect_result_timestamp::time
FROM t_product, t_job_number,
t_link_contract_number_job_number,
     t_serial_number, t_inspect, t_inspect_area,
t_inspect_result
WHERE t_product.product_id = t_job_number.product_id
AND t_job_number.job_number_id =
t_link_contract_number_job_number.job_number_id
AND
t_link_contract_number_job_number.link_contract_number_job_number_id
=
    t_serial_number.link_contract_number_job_number_id
AND t_serial_number.serial_number_id =
t_inspect.serial_number_id
AND t_inspect_area.inspect_area_id =
t_inspect.inspect_area_id
AND t_inspect.inspect_id = t_inspect_result.inspect_id

-- the statement below is *key*
AND t_inspect.inspect_pass = 'f'
-- the statement above is *key*

ORDER BY t_product.product_number ASC,
serial_number::integer ASC,
t_inspect_result.inspect_result_timestamp DESC

the purpose of the query is to a multi-dimensional
array to populate a series of linked select boxes. 
only those sets of values that haven't already passed
an inspection are listed, hence the 

AND t_inspect.inspect_pass = 'f'

line.

in order to be able to include...

AND t_inspect.inspect_pass = 'f'

...i had to repeat data (added inspect_pass column in
t_inspect) when that data already exists (the latest
date boolean value in
t_inspect_result.inspect_result_pass).

t_inspect.inspect pass records the current state of a
unit in inspection (pass or fail), while
t_inspect_result.inspect_result_pass records all
inspect results until a pass is achieved (eg, f, f, f,
f, f, f, f, f, t).

anyway, i want to incorporate a query that finds this
last date inspect_pass_result value and replace...

AND t_inspect.inspect_pass = 'f'

the query that pulls all the latest date values is...

SELECT DISTINCT ON (t_inspect.inspect_id)
t_inspect_result.inspect_result_pass, 
       t_inspect_result.inspect_result_id
  FROM t_inspect_result, t_inspect, t_serial_number,
t_product, t_job_number, 
       t_link_contract_number_job_number,
t_inspect_area
  WHERE t_inspect_result.inspect_id =
t_inspect.inspect_id
    AND t_inspect.serial_number_id =
t_serial_number.serial_number_id
    AND t_product.product_id = t_job_number.product_id
    AND t_job_number.job_number_id = 
       
t_link_contract_number_job_number.job_number_id
    AND
t_link_contract_number_job_number.link_contract_number_job_number_id
=
       
t_serial_number.link_contract_number_job_number_id
    AND t_serial_number.serial_number_id =
t_inspect.serial_number_id
    AND t_inspect_area.inspect_area_id =
t_inspect.inspect_area_id
    AND t_inspect.inspect_id =
t_inspect_result.inspect_id
  ORDER BY t_inspect.inspect_id,
inspect_result_timestamp DESC

tia...

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



view thread (2+ messages)  latest in thread

Message-ID: <20060602200640.91950.qmail@web33304.mail.mud.yahoo.com>
Permalink:  ../20060602200640.91950.qmail@web33304.mail.mud.yahoo.com/
Also on:    postgresql.org/message-id/20060602200640.91950.qmail@web33304.mail.mud.yahoo.com

 · 

reply

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: operationsengineer1@yahoo.com
  Subject: Re: Using Query Result in WHERE Clause
  In-Reply-To: <20060602200640.91950.qmail@web33304.mail.mud.yahoo.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