If I have a MySQL table such as:
I want to use SQL to calculate the sum of the PositiveResult
column and also the NegativeResult
column. Normally I could simply do SUM(PositiveResult)
in a query.
But what if I wanted to go a step further and place the totals in a row at the bottom of the result set:
Can this be achieved at the data level or is it a presentation layer issue? If it can be done by SQL, how might I do this? I am a bit of an SQL newbie.
Thanks to the respondents. I will now check things with the customer.
Also, can a text column be added so that the value of the last row of data is not shown in the summary row? Like this:
if you need the absolute value put sum(abs(NegativeResults)
I would also do this in the presentation layer, but you can do it MySQL...
Here's a hack for the amended problem - it ain't pretty but I think it works...
I'd recommend doing this at the presentation layer. To do something like this in SQL is also possible.
I added ordering using a arbitrarily high number if keywordid is null to make sure the ordered recordset can be pulled easily by the view for displaying.
This should be handled at least one layer above the SQL query layer.
The initial query can fetch the detail info and then the application layer can calculate the aggregation (summary row). Or, a second db call to fetch the summary directly can be used (although this would be efficient only for cases where the calculation of the summary is very resource-intensive and a second db call is really necessary - most of the time the app layer can do it more efficiently).
The ordering/layout of the results (i.e. the detail rows followed by the "footer" summary row) should be handled at the presentation layer.