I am running a MySql database through phpmyadmin and am working on building a query to total some values.
In EXCEL the formula looks like this =IF(AND(P6=P5,B6=B5),SUM(H5+F6),F6)
That formula is put in the H column
P Column is a class identifier. B Column is their registration number. H Column is their total points. F column is the point value of their entry.
It works great in excel. But having some troubles in the syntax for SQL.
I know that first I need to sort the file so that everything comes through in the correct order, got that. But from that point on I am clueless.
If a more in depth explanation is needed for my excel code I can do that, if it helps.
As far as I understand you need running total with some “partitioning” or rows. It seems that in MySql you need to do some “hacking” with variables, as analytic functions are not available in this database.
Following query will do it. It uses three variables – @rollingsum to store sum of F column, and @prevP and @prevB variables to store previous value of B and P columns)
Example:
Source data:
Result for the above query (rollingsum column is your H column – total points in your excel):