MySQL:创建regr_slope函数

时间:2015-06-04 06:46:54

标签: mysql

在MySQL中我添加了这个函数来计算斜率:

class GroupUserCreateView(CreateView):
    model = GroupUser
    fields = ['user', 'type']
    template_name = "group_user_create_form.html"

    def dispatch(self, request, *args, **kwargs):
        self.group = get_object_or_404(Group, id=self.kwargs['group_id'])
        return super(GroupUserCreateView, self).dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        form.instance.group = self.group
        return super(GroupUserCreateView, self).form_valid(form)

    def get_success_url(self):
        return reverse('group_user_list', kwargs={'group_id': self.group.id})

然而,当我像这样使用它时:

DELIMITER $$
CREATE FUNCTION regr_slope(x float, y float)
RETURNS float
DETERMINISTIC
BEGIN 
  DECLARE n int;
  DECLARE sum_x float;
  DECLARE sum_y float;
  DECLARE sum_xx float;
  DECLARE sum_xy float;
  DECLARE slope float;

  SET n = COUNT(x);
  SET sum_x = SUM(x);
  SET sum_y = SUM(y);
  SET sum_xx = SUM(x*x);
  SET sum_xy = SUM(x*y);

  SET slope = (n * sum_xy - sum_x * sum_y) / (n * sum_xx - POWER(sum_x, 2));

  RETURN slope;
END$$
DELIMITER ;

我收到错误无效使用群组功能。 这可能是什么原因?

0 个答案:

没有答案