Subject | RE: [firebird-support] Basics of Statistics - Mathematics: Mean (Average), Median, Mode and Range ? |
---|---|
Author | Roberto Carlos |
Post date | 2016-12-05T01:52:19Z |
Solved. Thanks.
──────────────────────────────────────────────────────────────────
De: "Svein Erling Tysvær setysvar@... [firebird-support]"
Enviada: 2016/12/01 08:18:14
Para: firebird-support@yahoogroups.com
Assunto: Re: [firebird-support] Basics of Statistics - Mathematics: Mean (Average), Median, Mode and Range ?
Don't know whether Firebird has internal functions for this or not and it is likely that there are more elegant solutions available, but the below queries should be ways to get what you ask (although I don't know whether you want to return all values for mode and whether or not you want the average in case of two median values).
execute block returns (mode integer) as
declare variable occurences integer;
declare variable dummy integer;
begin
select , count(*)
from
group by 1
order by 2 desc
rows 1
into :dummy, :occurences;
for select
from
group by 1
having count(*) = :occurences
into :mode do
suspend;
end
execute block returns (median decimal(9, 1)) as
declare variable occurences integer;
declare variable RowsFrom integer;
declare variable RowsTo integer;
begin
select count(*)
from
into :occurences;
RowsFrom = ((:occurences-1)/2)+1;
RowsTo = (:Occurences/2)+1;
with tmp(tmp) as
(select cast( as decimal(9, 1))
from
rows :RowsFrom to :RowsTo)
select sum(tmp)/(:RowsTo-:RowsFrom+1)
from tmp
into :Median;
suspend;
end
select max()-min() as Range
from
HTH,
Set