Re: update with from

From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Cc: Sim Zacks <sim(at)compulab(dot)co(dot)il>
Subject: Re: update with from
Date: 2012-01-23 16:52:06
Message-ID: 201201230852.06970.adrian.klaver@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Monday, January 23, 2012 7:32:35 am Sim Zacks wrote:
> On 01/23/2012 05:13 PM, Adrian Klaver wrote:
> > I guess the primary question here is, what are you trying to achieve?
> > Do want a particular row to supply the values to the target table i.e the
> > row with the most timestamp?
> > What is the query you are using?
>
> The query returns a partid, unitprice and delivery weeks from the latest
> set of rfqs sent. I want to update the table with the delivery weeks per
> part of the cheapest of those rfqs.

>
> This is the update stmt I am using, assuming that it always updates the
> table with the last row per part:
> update stat_allocated_components a set
> partarrivedate=current_date+(b.deliverywks*7),partarrivedate_source='RFQ
> Est'
> from
> (select b.popartid,b.partid,b.unitprice,b.deliverywks from poparts b
> join pos c using(poid)
> join lastrfqdateperpart d using(partid)
> where c.isrfq and c.issuedate > d.issuedate-7
> AND b.unitprice > 0::numeric AND b.quantity >= 100::numeric AND
> c.postatusid = ANY (ARRAY[40, 41])
> order by b.partid,b.unitprice desc, b.deliverywks desc) b
> where a.partid=b.partid and partarrivedate is null and
> a.stock-a.previouscommitmentlf+a.quantity<0 and b.deliverywks is not null
>
> This query take 163 ms.
>
> When I throw in code to make the select only return the correct rows
> The select statement takes 9 secs by itself:
> select a.partid,a.deliverywks
> from poparts a where popartid in (
> select b.popartid from poparts b
> join pos c using(poid)
> join stock.lastrfqdateperpart d using(partid)
> where c.isrfq and c.issuedate > d.issuedate-7
> AND b.unitprice > 0::numeric AND b.quantity >= 100::numeric AND
> c.postatusid = ANY (ARRAY[40, 41])
> and b.partid=a.partid
> order by b.partid,b.unitprice, b.deliverywks
> limit 1
> )

From what I can see they are not the same queries, notwithstanding the
selectivity in the second query. In fact I am not sure what the second query
accomplishes that cannot be done in the first query:)

Would you not get the same result in the first query by doing something like:

select b.popartid,b.partid,b.unitprice,b.deliverywks from poparts b
join pos c using(poid)
join lastrfqdateperpart d using(partid)
where c.isrfq and c.issuedate > d.issuedate-7
AND b.unitprice > 0::numeric AND b.quantity >= 100::numeric AND
c.postatusid = ANY (ARRAY[40, 41])
order by b.partid,b.unitprice desc, b.deliverywks desc limit 1

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2012-01-23 17:10:19 Re: update with from
Previous Message David Johnston 2012-01-23 16:32:41 Re: [RFE] auto ORDER BY for SELECT