Обсуждение: Fw: Clipping values

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

Fw: Clipping values

От
Ovid
Дата:
In response to a private email from someone else on this list, I thought I should send the following clarification.

I have a table with four values which are constantly lowered by user action. These four values must rise over time, in
fiveminute intervals.  The core of the function, in pseudo-code, would look something like this: 

  FOREACH row IN SELECT * FROM some_table WHERE some_percentages are less than 1
  LOOP
      new_percentage = num_intervals * .1 > 1
          ? 1
          : num_intervals * .1;
      UPDATE some_table SET some_percentages = new_percentage WHERE id = row.id
  END LOOP

But, of course, doing that for four percentages (with at least one having a different increment value than .1)

Cheers,
Ovid



Re: Fw: Clipping values

От
Tom Lane
Дата:
Ovid <curtis_ovid_poe@yahoo.com> writes:
> I have a table with four values which are constantly lowered by user action. These four values must rise over time,
infive minute intervals.  The core of the function, in pseudo-code, would look something like this: 

>   FOREACH row IN SELECT * FROM some_table WHERE some_percentages are less than 1
>   LOOP
>       new_percentage = num_intervals * .1 > 1
>           ? 1
>           : num_intervals * .1;
>       UPDATE some_table SET some_percentages = new_percentage WHERE id = row.id
>   END LOOP

> But, of course, doing that for four percentages (with at least one having a different increment value than .1)

You could code that directly with CASE operations, but it would probably
be easier to use GREATEST/LEAST, along the lines of
    new_percentage = LEAST(num_intervals * .1, 1);

            regards, tom lane

Re: Fw: Clipping values

От
Ovid
Дата:
----- Original Message ----
> From: Tom Lane <tgl@sss.pgh.pa.us>


> You could code that directly with CASE
> operations, but it would probably
> be easier to use GREATEST/LEAST, along the
> lines of
>    new_percentage = LEAST(num_intervals * .1,
> 1);


Ah, I didn't know about the LEAST function! I love you (platonically, of course. My fiancée might have issues
otherwise).

Cheers,
Ovid
--
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog - http://blogs.perl.org/users/ovid/
Twitter - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6