DIfference between max() and greatest() ?

Поиск
Список
Период
Сортировка
Искать

DIfference between max() and greatest() ?

От:
"Ruben Gouveia" <rubes7202@gmail.com>
Дата:
What is the difference between these two. I know that max() is an aggregate function

select model,count(distinct cars)
from rc_cars
where greatest(baught,returned) < current_date - integer '1'
group by model;

Do i need to have a max () around a greatest() to make sure i get the most recent of the two.

select model,count(distinct cars)
from rc_cars
where max(greatest(baught,returned)) < current_date - integer '1'
group by model;


Re: DIfference between max() and greatest() ?

От:
"Bart Degryse" <Bart.Degryse@indicator.be>
Дата:
max(expression) is an aggregate function
(from the manual: "maximum value of expression across all input values")
greatest(value [, ...]) is not an aggregate function
(from the manual: "The GREATEST function selects the largest value from a list of any number of expressions.")
 
So max takes the maximum of values coming from several rows, while greatest takes the maximum of values coming from 1 row.
 
Assuming
select model,count(distinct cars)
from rc_cars
group by model
returns more than one record you will need both functions.
Greatest gets the most recent date out of "bought" and "returned" PER RECORD.
Max gets the most recent date out of all these greatest dates OVER ALL RECORDS. 
 
 
ModelBoughtReturned
X2004-08-252005-01-01->Greatest = 2005-01-01
X2006-02-172006-02-18->Greatest = 2006-02-18
X2005-11-132001-05-16->Greatest = 2005-11-13
¯
Max = 2006-02-18
 
That being said, isn't it unlikely that "bought" is more recent than "returned"?
I can imagine that one can only return a car after buying it.
If so, writing
... WHERE max(returned) < current_date - interval '1 day' ...
 would be enough.
Another thought: with this WHERE clause a car returned yesterday will not show up.
Is that what you want? If not, use
... WHERE max(returned) < current_date ...
 
Good luck


>>> "Ruben Gouveia" rubes7202@gmail.com> 2008-09-11 19:33 >>
What is the difference between these two. I know that max() is an aggregate function

select model,count(distinct cars)
from rc_cars
where greatest(baught,returned) < current_date - integer '1'
group by model;

Do i need to have a max () around a greatest() to make sure i get the most recent of the two.

select model,count(distinct cars)
from rc_cars
where max(greatest(baught,returned)) < current_date - integer '1'
group by model;


Re: DIfference between max() and greatest() ?

От:
Peter Eisentraut <peter_e@gmx.net>
Дата:
Ruben Gouveia wrote:
> What is the difference between these two. I know that max() is an 
> aggregate function

... and greatest() is a normal single-row function.

One works vertically, one works horizontally, if that helps you. :-)

Or max() is like sum() and greatest is like +.


Pls Hlp: SQL Problem

От:
Hengky Lie <hengkyliwandouw@gmail.com>
Дата:
 Dear friends,

I have problem in postgresql  view.  This is the view (Pay attention to the red and blue colours):

SELECT tblpenjualan.tanggal, tblpenjualan."operator", tblpenjualan.noinvoice, tblpenjualan.bayar, tblpenjualan.bayarvocher, tblpenjualan.jam, tblpenjualan.id, tblpenjualan.jeniscard, tbltransaksi.kodeproduk, tbltransaksi.keluar, tbltransaksi.harga, tbltransaksi.disc1, tblproduk.namabarang, tblproduk.subkat, tblsubkategori.subkategori, tblkategoriproduk.kode, tblkategoriproduk.kategori, tbltransaksi.hargapokok * tbltransaksi.keluar AS modal, tbltransaksi.keluar * tbltransaksi.harga - tbltransaksi.disc1 / 100::numeric * (tbltransaksi.keluar * tbltransaksi.harga) AS jumlah, tbltransaksi.keluar * tbltransaksi.harga - tbltransaksi.disc1 / 100::numeric * (tbltransaksi.keluar * tbltransaksi.harga) - tbltransaksi.hargapokok * tbltransaksi.keluar AS profit
   FROM tblpenjualan
   JOIN tbltransaksi ON tblpenjualan.id = tbltransaksi.jualid
   LEFT JOIN tblproduk ON tbltransaksi.kodeproduk::text = tblproduk.kode::text
   LEFT JOIN tblsubkategori ON tblproduk.subkat::text = tblsubkategori.id::text
   LEFT JOIN tblkategoriproduk ON tblsubkategori.kategoriid::text = tblkategoriproduk.kode::text;

The result is :

"2008-07-20";"ULY";"01-200708";"Tunai";0.00;"1899-12-30 21:14:47+06:55:25";1;"";"8993417200021";1.00;4200.00;0.00;"ELIPS HAIR VITAMIN ";"121";"VITAMIN RAMBUT";"NF";"Non Food";3960.0000;4200.000000000000000000000000;240.000000000000000000000000
"2008-07-20";"ULY";"01-200708";"Tunai";0.00;"1899-12-30 21:14:47+06:55:25";1;"";"8991001111425";3.00;1650.00;0.00;"TAKE-IT MILK CHOCOLATE WAFER 17 G";"8";"BISKUIT";"MKN";"Makanan";4680.0000;4950.000000000000000000000000;270.000000000000000000000000
"2008-07-20";"ULY";"01-200708";"Tunai";0.00;"1899-12-30 21:14:47+06:55:25";1;"";"8991115000103";2.00;1600.00;0.00;"ALPENLIEBE ORIGINAL 41 G ISI 10 BTR";"62";"PERMEN";"MKN";"Makanan";3000.0000;3200.000000000000000000000000;200.000000000000000000000000
"2008-07-20";"ULY";"01-200708";"Tunai";0.00;"1899-12-30 21:14:47+06:55:25";1;"";"8990800010533";1.00;1600.00;0.00;"MENTOS CHEWY DRAGEES 37 G";"62";"PERMEN";"MKN";"Makanan";1500.0000;1600.000000000000000000000000;100.000000000000000000000000

Why  the calculation result has so many decimals ?

Thank you

Re: Pls Hlp: SQL Problem

От:
Richard Huxton <dev@archonet.com>
Дата:
Hengky Lie wrote:
> Dear friends,

Hello. Quick tip - don't reply to another message when starting a new
thread, people will assume it's part of the old thread.

[snip most of query]
> / 100::numeric * 

> Food";3960.0000;*4200.000000000000000000000000*;*240.000000000000000000000000*

> Why  the calculation result has so many decimals ?

Because you've cast 100 to numeric, rather than numeric(10,2) or
whatever. This means you get the full accuracy on the calculation - it
won't ever trim a numeric (that's the whole reason to have the type).

--  Richard Huxton Archonet Ltd

Re: DIfference between max() and greatest() ?

От:
"Ruben Gouveia" <rubes7202@gmail.com>
Дата:
Thanks Bart,

Your explanation is great. Returned and Bought are of datatypes date, hence Returned would be a more recent value.



On Fri, Sep 12, 2008 at 12:10 AM, Bart Degryse <Bart.Degryse@indicator.be> wrote:
max(expression) is an aggregate function
(from the manual: "maximum value of expression across all input values")
greatest(value [, ...]) is not an aggregate function
(from the manual: "The GREATEST function selects the largest value from a list of any number of expressions.")
 
So max takes the maximum of values coming from several rows, while greatest takes the maximum of values coming from 1 row.
 
Assuming
select model,count(distinct cars)
from rc_cars
group by model
returns more than one record you will need both functions.
Greatest gets the most recent date out of "bought" and "returned" PER RECORD.
Max gets the most recent date out of all these greatest dates OVER ALL RECORDS. 
 
 
ModelBoughtReturned
X2004-08-252005-01-01->Greatest = 2005-01-01
X2006-02-172006-02-18->Greatest = 2006-02-18
X2005-11-132001-05-16->Greatest = 2005-11-13
¯
Max = 2006-02-18
 
That being said, isn't it unlikely that "bought" is more recent than "returned"?
I can imagine that one can only return a car after buying it.
If so, writing
... WHERE max(returned) < current_date - interval '1 day' ...
 would be enough.
Another thought: with this WHERE clause a car returned yesterday will not show up.
Is that what you want? If not, use
... WHERE max(returned) < current_date ...
 
Good luck


>>> "Ruben Gouveia" rubes7202@gmail.com> 2008-09-11 19:33 >>
What is the difference between these two. I know that max() is an aggregate function

select model,count(distinct cars)
from rc_cars
where greatest(baught,returned) < current_date - integer '1'
group by model;

Do i need to have a max () around a greatest() to make sure i get the most recent of the two.

select model,count(distinct cars)
from rc_cars
where max(greatest(baught,returned)) < current_date - integer '1'
group by model;



Re: Pls Hlp: SQL Problem

От:
Hengky Lie <hengkyliwandouw@gmail.com>
Дата:
 Dear Richard,

Sorry for that :) and thanks for your tips, but it didn't solve my problem. Maybe because my postgresql knowledge limitation. I have tried your suggestion, my view like this :

-- View: "vwlapjual"

-- DROP VIEW vwlapjual;

CREATE OR REPLACE VIEW vwlapjual AS
 SELECT tblpenjualan.tanggal, tblpenjualan."operator", tblpenjualan.noinvoice, tblpenjualan.bayar, tblpenjualan.bayarvocher, tblpenjualan.jam, tblpenjualan.id, tblpenjualan.jeniscard, tbltransaksi.kodeproduk, tbltransaksi.keluar, tbltransaksi.harga, tbltransaksi.disc1, tblproduk.namabarang, tblproduk.subkat, tblsubkategori.subkategori, tblkategoriproduk.kode, tblkategoriproduk.kategori, tbltransaksi.hargapokok * tbltransaksi.keluar::numeric(2,0) AS modal, tbltransaksi.keluar * tbltransaksi.harga - (tbltransaksi.disc1 / 100::numeric(3,0) * (tbltransaksi.keluar * tbltransaksi.harga))::numeric(10,0) AS jumlah, tbltransaksi.keluar * tbltransaksi.harga - tbltransaksi.disc1 / 100::numeric(3,0) * (tbltransaksi.keluar * tbltransaksi.harga) - (tbltransaksi.hargapokok * tbltransaksi.keluar)::numeric(10,0) AS profit
   FROM tblpenjualan
   JOIN tbltransaksi ON tblpenjualan.id = tbltransaksi.jualid
   LEFT JOIN tblproduk ON tbltransaksi.kodeproduk::text = tblproduk.kode::text
   LEFT JOIN tblsubkategori ON tblproduk.subkat::text = tblsubkategori.id::text
   LEFT JOIN tblkategoriproduk ON tblsubkategori.kategoriid::text = tblkategoriproduk.kode::text;

ALTER TABLE vwlapjual OWNER TO hpl;

and the view result like this :

"2008-07-20";"ULY";"01-200708";"Tunai";0.00;"1899-12-30 21:14:47+06:55:25";1;"";"8993417200021";1.00;4200.00;0.00;"ELIPS HAIR VITAMIN ";"121";"VITAMIN RAMBUT";"NF";"Non Food";3960.00;4200.0000;240.000000000000000000000000

Thanks for any suggestion.

Regards,
Hengky


Richard Huxton wrote:
Hengky Lie wrote: 
Dear friends,   
Hello. Quick tip - don't reply to another message when starting a new
thread, people will assume it's part of the old thread.

[snip most of query] 
/ 100::numeric *    
 
Food";3960.0000;*4200.000000000000000000000000*;*240.000000000000000000000000*   
 
Why  the calculation result has so many decimals ?   
Because you've cast 100 to numeric, rather than numeric(10,2) or
whatever. This means you get the full accuracy on the calculation - it
won't ever trim a numeric (that's the whole reason to have the type).
 

Re: Pls Hlp: SQL Problem

От:
"Scott Marlowe" <scott.marlowe@gmail.com>
Дата:
On Fri, Sep 12, 2008 at 12:50 PM, Hengky Lie  wrote:
> Dear Richard,
>
> Sorry for that :) and thanks for your tips, but it didn't solve my problem.
> Maybe because my postgresql knowledge limitation. I have tried your
> suggestion, my view like this :

Put parens around the whole thing, like:

(tbltransaksi.hargapokok * tbltransaksi.keluar)::numeric(2,0) AS modal

Otherwise you're really doing this:

tbltransaksi.hargapokok::numeric * tbltransaksi.keluar::numeric(2,0) AS modal

and when you multiply a numeric times a numeric(2,0) you get a
numeric, not a numeric(2,0)

Re: Pls Hlp: SQL Problem

От:
"Fernando Hevia" <fhevia@ip-tel.com.ar>
Дата:
 

> -----Mensaje original-----
> De: pgsql-sql-owner@postgresql.org 
> [mailto:pgsql-sql-owner@postgresql.org] En nombre de Scott Marlowe
> 
> On Fri, Sep 12, 2008 at 12:50 PM, Hengky Lie 
>  wrote:
> > Dear Richard,
> >
> 
> Put parens around the whole thing, like:
> 
> (tbltransaksi.hargapokok * tbltransaksi.keluar)::numeric(2,0) AS modal
> 

Consider that if you are NOT going to use the decimals you should really use
integer or bigint datatypes. The numeric type compute much slower than
integer datatypes.

Regards,
Fernando


Re: Pls Hlp: SQL Problem

От:
"Scott Marlowe" <scott.marlowe@gmail.com>
Дата:
On Fri, Sep 12, 2008 at 4:01 PM, Fernando Hevia  wrote:
>
>
>> -----Mensaje original-----
>> De: pgsql-sql-owner@postgresql.org
>> [mailto:pgsql-sql-owner@postgresql.org] En nombre de Scott Marlowe
>>
>> On Fri, Sep 12, 2008 at 12:50 PM, Hengky Lie
>>  wrote:
>> > Dear Richard,
>> >
>>
>> Put parens around the whole thing, like:
>>
>> (tbltransaksi.hargapokok * tbltransaksi.keluar)::numeric(2,0) AS modal
>>
>
> Consider that if you are NOT going to use the decimals you should really use
> integer or bigint datatypes. The numeric type compute much slower than
> integer datatypes.

Note that if you're just converting the output the cost is minimal
compared to if you're doing all your math in numeric.  It's when you
force math to happen that numeric is slower, but numeric's gotten a
lot of tuning in the last few years and it's withing a few percentage
of integer for most measurements.   Definitely not twice as slow or
anything like they once were.

Re: Pls Hlp: SQL Problem

От:
"Scott Marlowe" <scott.marlowe@gmail.com>
Дата:
On Mon, Sep 15, 2008 at 2:14 PM, Fernando Hevia  wrote:
>
>
>> -----Mensaje original-----
>> De: Scott Marlowe [mailto:scott.marlowe@gmail.com]
>
>> >
>> > Consider that if you are NOT going to use the decimals you should
>> > really use integer or bigint datatypes. The numeric type
>> compute much
>> > slower than integer datatypes.
>>
>> Note that if you're just converting the output the cost is
>> minimal compared to if you're doing all your math in numeric.
>>  It's when you force math to happen that numeric is slower,
>> but numeric's gotten a lot of tuning in the last few years
>> and it's withing a few percentage
>> of integer for most measurements.   Definitely not twice as slow or
>> anything like they once were.
>>
>
> Well, in that case the manual should be revised.
>
> 8.1.2. Arbitrary Precision Numbers
>
> The type numeric can store numbers with up to 1000 digits of precision and
> perform calculations exactly. It is especially recommended for storing
> monetary amounts and other quantities where exactness is required. However,
> arithmetic on numeric values is __very slow__ compared to the integer types,
> or to the floating-point types described in the next section.
>
> Ref: http://www.postgresql.org/docs/current/static/datatype-numeric.html
>
> The explicit "very slow" assertion scared me quite enough so to avoid
> numeric types where possible.

yeah, I agree.  In the days of 7.2 it was horrifically slow (like 10
to 20 times slower most the time) to do numeric math on similar sized
numbers as int math.

Note that if you are working with numbers of 500 digits it's gonna be
a lot slower than numbers with 10 digits, but if the numbers are
similar in size, numeric has had a lot of optimization that makes it
quite usable now.  which is handy when porting from oracle, where
everything is a sort-of numeric.

Re: Pls Hlp: SQL Problem

От:
"Fernando Hevia" <fhevia@ip-tel.com.ar>
Дата:
 

> -----Mensaje original-----
> De: Scott Marlowe [mailto:scott.marlowe@gmail.com] 

> >
> > Consider that if you are NOT going to use the decimals you should 
> > really use integer or bigint datatypes. The numeric type 
> compute much 
> > slower than integer datatypes.
> 
> Note that if you're just converting the output the cost is 
> minimal compared to if you're doing all your math in numeric. 
>  It's when you force math to happen that numeric is slower, 
> but numeric's gotten a lot of tuning in the last few years 
> and it's withing a few percentage
> of integer for most measurements.   Definitely not twice as slow or
> anything like they once were.
> 

Well, in that case the manual should be revised.

8.1.2. Arbitrary Precision Numbers

The type numeric can store numbers with up to 1000 digits of precision and
perform calculations exactly. It is especially recommended for storing
monetary amounts and other quantities where exactness is required. However,
arithmetic on numeric values is __very slow__ compared to the integer types,
or to the floating-point types described in the next section. 

Ref: http://www.postgresql.org/docs/current/static/datatype-numeric.html

The explicit "very slow" assertion scared me quite enough so to avoid
numeric types where possible.

Regards,
Fernando.


FAQ