0

I never worked with pgsql before and im trying to convert this function to Mysql... can you give me a hand?

create or replace function hot(ups integer, downs integer, date timestamp with time zone) returns numeric as $$
select round(cast(log(greatest(abs($1 - $2), 1)) + sign($1 - $2) * (date_part('epoch', $3) - 1134028003) / 45000.0 as numeric), 7) $$ language sql immutable

EDIT:

So far i've got this but it still gives me an error

create function hot(ups MEDIUMINT, downs MEDIUMINT, date timestamp) returns BIGINT
select round(cast(log(greatest(abs($1 - $2), 1)) + sign($1 - $2) * (unix_timestamp(date) - 1134028003) / 45000.0 BIGINT), 7)
$$ language sql immutable

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BIGINT), 7) $$ language sql immutable' at line 2"

1

1 Answer 1

1

That defines the function hot(ups, downs, date) (where ups and downs are integers and date is a timestamp-with-time-zone) as this SQL statement:

SELECT ROUND( CAST( LOG(GREATEST(ABS(ups - downs), 1))
                    + SIGN(ups - downs) * (DATE_PART('epoch', date) - 1134028003) / 45000.0
                    AS NUMERIC
                  ),
              7
            )
;

The $$ ... $$ notation is a PostgreSQL notation for a multiline string (in this case, containing the SQL query that defines the function). The LANGUAGE SQL indicates that the function is in SQL (and not, for example, PL/pgSQL). The IMMUTABLE (see http://www.postgresql.org/docs/8.4/static/sql-createfunction.html) indicates that the function doesn't modify the database, and always returns the same results for the same arguments. (In other words, it's a "pure" function.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.