Re: [SQL] Can I do this smarter?

2006-07-14 Thread Joost Kraaijeveld
This is clearly the "Aaron helps day" ;-)

Thanks,

-- 
Groeten,

Joost Kraaijeveld
Askesis B.V.
Molukkenstraat 14
6524NB Nijmegen
tel: 024-3888063 / 06-51855277
fax: 024-3608416
web: www.askesis.nl

---(end of broadcast)---
TIP 6: explain analyze is your friend


Re: [SQL] Can I do this smarter?

2006-07-14 Thread Aaron Bingham

Aaron Bono wrote:

On 7/13/06, *Joost Kraaijeveld* <[EMAIL PROTECTED] 
> wrote:


I have three tables: customers, salesorders and invoices.
Customers have
salesorders and salesorders have invoices ( child tables have foreign
key columns to their parent).

I want to get a list of all invoices with their customers. This
what I
came up with:

select
invoices.objectid,
invoices.invoicenumber,
invoices.invoicedate,
(select customer from salesorders where objectid =
invoices.salesorderobjectid),
(select customernumber from customers where objectid = (select
customer from salesorders where objectid =
invoices.salesorderobjectid)),
(select lastname from customers where objectid = (select customer
from salesorders where objectid = invoices.salesorderobjectid))
from invoices

Can I do this smarter as the three subselects select the same
customer three times and I would think that 1 time is enough? 



 
SELECT

   invoices.objectid,
   invoices.invoicenumber,
   invoices.invoicedate,
   salesorders.customer,
   customers.customernumber,
   customers.lastname
FROM invoices
INNER JOIN salesorders ON (
   salesorders.objectid = invoices.salesorderobjectid
)
INNER JOIN customers ON (
   customers.objectid = salesorder.customer
)

You should do INNER and OUTER  joins for connecting the tables by 
their foreign keys.


You can also rewirite this (IMO) more clearly thus:

SELECT
  invoices.objectid,
  invoices.invoicenumber,
  invoices.invoicedate,
  salesorders.customer,
  customers.customernumber,
  customers.lastname
FROM invoices, salesorders, customers
WHERE salesorders.objectid = invoices.salesorderobjectid
  AND customers.objectid = salesorder.customer;

--

Aaron Bingham
Senior Software Engineer
Cenix BioScience GmbH



---(end of broadcast)---
TIP 3: Have you checked our extensive FAQ?

  http://www.postgresql.org/docs/faq


[SQL] INSERT ... SELECT FROM .... FOR UPDATE?

2006-07-14 Thread Mark Stosberg


Hello,

This is a re-phrasing of question I asked recently, inquiring about a
an approach I'm considering.

My query is:

> INSERT into item_hit_log (item_id, hit_date, hit_count)
>SELECT item_id, CURRENT_DATE + CAST('1 day' AS interval), 0
> FROM items where item_state = 'available';

The "items" table has a few hundred thousand rows in it, and is likely
to be updated by other queries while this runs.

The error we got last night was:

  ERROR:  insert or update on table "item_hit_log" violates foreign key
constraint "item_id_fk"
  DETAIL:  Key (item_id)=(451226) is not present in table "items".

Re-running the transaction block a few minutes later worked.

 From reading the docs, it sounds like "SELECT ... FOR UPDATE"
might be the perfect solution here.

http://www.postgresql.org/docs/8.1/static/sql-select.html#SQL-FOR-UPDATE-SHARE

Since it's hard to test for this kind of failure, I wanted to get the
opinions of others here if this would be a good idea.

Thanks!

Mark


---(end of broadcast)---
TIP 4: Have you searched our list archives?

  http://archives.postgresql.org