Home > database >  Consult about the use of stored procedures in the query to calculate the question.
Consult about the use of stored procedures in the query to calculate the question.

Time:09-29

Such as a table there is a goods monthly sales
Soap 50 201801
Soap 31 201802
Soap 33 201803
Soap, 201804
.,,,
Soap 201811 1
Soap 21 201812

Now need you to calculate a new value is the accumulation of monthly sales this year
Soap 50 201801
Soap, 201802, 81,
Soap, 201803, 114,
Soap, 201804, 114,

How to use the way of query results
Will use stored procedures,,,
I think a lot of methods but with more and more complicated

CodePudding user response:

Table with id field right
Reference before the great god give me the solution of
http://bbs.csdn.net/topics/392315964
 
Update the demo table set total_cost=(select sum (cost) from (select * from the demo table) b where b. date & gt; The demo table. Date);

CodePudding user response:

The stored procedure, you just need to add the SQL statement to error process

CodePudding user response:

If only the query
 - all the accumulative 
SELECT DATA. * @ total:=@ total + products generate as cumulative
FROM the DATA of TB, (SELECT @ total:=0) _x
Date of the ORDER BY commodity,
;
- the goods separate cumulative
SELECT DATA. * @ total:=products generate + the IF (@ sp=goods, @ total, 0) as the cumulative, @ sp:
=goodsFROM the DATA of TB, (SELECT @ total:=0, @ sp:=null) _x
Date of the ORDER BY commodity,
;

If you want to update to the table, chang the query as a sub queries and to update the table
  • Related