views:

729

answers:

5

I have a table with a credit and debit column.

I need to get the highest balance out of that, and I think a stored procedure is the way to do it, but I have no idea how.

I need to start with the first row, add the debits, subtract the credits and store the value A.

Second row is A+debit-credit=B; A = max(A,B) Repeat last step till the end.

Remember, I'm looking for the highest EVER, not the current, which would just be sum(debit-credit)

A: 

May

select max(debit-credit) from yourtable

work?

Try to test it.

friol
No, all this does is give me the highest single charge ever made, it doesn't consider what the balance is after that charge was made.
+1  A: 

It seems to me that you'd want the running total to be A+credit-debit but switch them if needed.

SET @balance := 0;
SET @high := 0;
SELECT @high := GREATEST(@balance := @balance+credit-debit, @high) FROM mytable;
SELECT @high;

edit: In response to your comment about forming a stored function from this... unlike stored procedures, stored functions must return one value, so they can't contain a SELECT query unless the query stores its result in a variable. This means the query must be guaranteed to have a single-value result. Below is a function I got to work, because in this case you only want the MAX value of @high anyway:

CREATE FUNCTION high_bal() RETURNS DECIMAL
BEGIN
  SET @balance := 0;
  SET @high := 0;

  SELECT MAX(@high := GREATEST(@balance := @balance+debit_acc-credit_acc, @high))
  INTO @high
  FROM credit_acc where credit_used_acc=63395;

  RETURN @high;
END$$
Bill Karwin
That did the job, but I'm having trouble making it a procedure, see below.
A: 

Actually, I thought I was going to be smart and make it a function, because I only want one value as the result...

DELIMITER $$

DROP FUNCTION IF EXISTS cc.high_bal$$

CREATE FUNCTION cc.high_bal() RETURNS DECIMAL

BEGIN
SET @balance := 0;
SET @high := 0;
SELECT @high := GREATEST(@balance := @balance+debit_acc-credit_acc, @high) FROM credit_acc where credit_used_acc=63395;

RETURN @high;
END$$

print("code sample");

DELIMITER ;

But that gives me the error " Not allowed to return a result set from a function" Which I don't understand because I do just want the single value.

See my edited response above.
Bill Karwin
A: 

User Defined Functions are only things that you embed within an SQL statement. They operate in the context of each single row (e.g. 40 rows, the function is executed 40 times). If you want an aggregate from a set (multiple rows), you use an SQL statement with an aggregate function in it (e.g. MAX(columname)). And that returns a "result set", which happens to be one row with one value.

le dorfier
A: 

Could anyone help me please - I need the below code in stored procedure format,its too urgent i have a deadline

function scormtrack($user,$courid) { $scormfile= get_records_sql('SELECT * FROM mdl_grade_grades gg, mdl_grade_items gi, mdl_scorm sc, mdl_scorm_scoes_track ssc WHERE gg.userid ='.$user.' AND usermodified ='.$user.' AND gi.courseid ='.$courid.' AND gi.id = gg.itemid AND sc.id = gi.iteminstance AND ssc.scormid = sc.id AND ssc.userid ='.$user.'');

$sq='SELECT * FROM mdl_grade_grades gg, mdl_grade_items gi, mdl_scorm sc, mdl_scorm_scoes_track ssc WHERE gg.userid ='.$user.' AND usermodified ='.$user.' AND gi.courseid ='.$courid.' AND gi.id = gg.itemid AND sc.id = gi.iteminstance AND ssc.scormid = sc.id AND ssc.userid ='.$user.''; echo $sq; $sq='SELECT * FROM mdl_grade_grades gg, mdl_grade_items gi, mdl_scorm sc, mdl_scorm_scoes_track ssc WHERE gg.userid ='.$user.' AND usermodified ='.$user.' AND gi.courseid ='.$courid.' AND gi.id = gg.itemid AND sc.id = gi.iteminstance AND ssc.scormid = sc.id AND ssc.userid ='.$user.''; echo $sq;

foreach($scormfile as $scormval) {

  $userid=$scormval->userid;
  $scormid=$scormval->scormid;
  $attempt=$scormval->attempt;
  $element=$scormval->element;
  $value=$scormval->value;
  $timemodified=$scormval->timemodified;

  $scoid=$scormval->scoid;

 $sqlabc="INSERT INTO mdl_scorm_scoes_tract_bk  (userid,scormid,scoid,attempt,element,value,timemodified) values($userid,$scormid,$scoid,$attempt,'$element','$value',$timemodified)";
 echo $sqlabc;
  $myquresult = mysql_query($sqlabc ) or die ('MySQL Error processing courses' . mysql_error());

  $delsql='DELETE FROM mdl_scorm_scoes_track where userid='.$userid.' AND scormid='.$scormid.'';

  echo $delsql;
  $myquresult = mysql_query($delsql ) or die ('MySQL Error processing courses' . mysql_error());

}

}

lavanya
I guess your comment was lost for not making it a question...
pascal