Re: obtaining difference between minimum value and next in size
От | Oliveiros d'Azevedo Cristina |
---|---|
Тема | Re: obtaining difference between minimum value and next in size |
Дата | |
Msg-id | 684FFBCA0637413987A29036F013A816@marktestcr.marktest.pt обсуждение исходный текст |
Ответ на | obtaining difference between minimum value and next in size ("John Lister" <john.lister-ps@kickstone.com>) |
Список | pgsql-sql |
John,
Great to hear it helped you out
Best,
Oliver
----- Original Message -----From: John ListerSent: Wednesday, November 17, 2010 10:46 PMSubject: Re: [SQL] obtaining difference between minimum value and next in sizeCheers oliverios and tom for your speedy replies. Unfortunately using v8.3 so the new functions are out. A big credit to oliverios for his sql fu, that seems to do exactly what I want and I think I pretty much understand the query. I always forget the comparison on the rows when thinking about groups.John----- Original Message -----Sent: Wednesday, November 17, 2010 4:09 PMSubject: Re: [SQL] obtaining difference between minimum value and next in sizeHi, John.I am not familiar with the functions Tom's indicated and I'm sure they constitute a much more straightfoward to solve your problem.Meanwhile, if you'd like to solve it with just SQL give this a try and see if it gives you the result you wantBest,OliveirosSELECT product_id, MIN(pv2) - pv1
FROM ((
SELECT product_id,MIN(price) as pv1
FROM offers
GROUP BY product_id) firstSubQuery
NATURAL JOIN
(
SELECT product_id,price as pv2
FROM offers) secondSubQuery
) total
WHERE pv1 <> pv2
GROUP BY product_id,pv1----- Original Message -----From: John ListerSent: Wednesday, November 17, 2010 3:11 PMSubject: [SQL] obtaining difference between minimum value and next in sizeHi, I was wondering if it is possible to do this with a single query rather than iterate over all of the rows in an application:I have a table which for brevity looks like:create table offers {integer id;integer product_id;double price;}where for each product there is a number of offers in this table. Now my question:Is it possible to obtain the difference between just the minimum price and the next one up per product, so say I have the following data:id, product_id, price123, 2, 10.01125, 2, 10.05128, 2, 11.30134, 3, 9.45147, 3, 11.42157, 3, 12.08167, 3, 12.09then I would like the following returnedproduct_id, difference2, .04 (10.05-10.01)3, 1.97 (11.42-9.45),etcAny ideas?ThanksJohn--Got needs? Get Goblin'! - http://www.pricegoblin.co.uk/
В списке pgsql-sql по дате отправления: