MySQL Forums
Forum List  »  General

create function help
Posted by: stephen bond
Date: November 19, 2009 12:28PM

I am trying to create a function to return a field from the last row from a group based on a certain ordering column. Please, help. This is just to save myself typing, using the expression in a query works fine.

DELIMITER $$

DROP FUNCTION IF EXISTS `test`.`last`$$
CREATE FUNCTION `test`.`last`(f1 varchar(12),f2 varchar(12)) RETURNS varchar(12) CHARSET latin1
reads sql data
return substring_index(group_concat(f1 order by f2 desc),',',1);

$$

DELIMITER ;

as example the follwoing works fine:
select acc,max(yearmon) as ym,matd, substring_index(group_concat(disc order by yearmon desc),',',1) as disc from longacc_hist group by acc;

Options: ReplyQuote


Subject
Written By
Posted
create function help
November 19, 2009 12:28PM
November 21, 2009 11:58AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.