One database collects all students and classes as well as other details on the specific class. In GUI report I need a SUM of all classes held until the end of a 1st period. My question is, do you in such situation save these results in a separate table like ReportFirstPeriod or you NEVER save such results because SUM can always be achieved via certain method in the code?
All statements about absolutes are always never true, most of the time. In general, you should not store a value that can be easily calculated. There are exceptions, but from the description of your situation, I can't really tell if this is one of those times. Here's some more general rules for using an exception: 1. The data itself will change, thus not lending itself to making your calculation in the future. For example, if you had an inventory table that simply had products and quantity, to get inventory on a certain date you would have to calculate that every day and store that data because your initial table has no date field. 2. Its more efficient to store the value. If the number of times that value is needed multiplied by the amount of time it takes to calculate it is signfigant then you might want to store the value in a table and just retrieve it. For example, you have a database of one hundred thousand gazillion records and every .8 seconds that value is requested then it would be easier to calculate once and store it rather than running summation query every time. Most likely, in your specific situation, you shouldn't store the value. If you provide a more thorough explanation of your situation I can be more specific.
If data is too huge and sum involves based on too many conditions and calculations, then pre-process it and store in a table, otherwise, calculate it in query as its not much of a load anyways.
In my opinion, it will be good to store the sum in a separate table. In case, that there are changes in your main database and need to recalculate you can run a simple query to update the Sum in a separate table..
As others have said, the main question is how frequently will this query be run? What sort of variable are you using as your Group By statement? The secondary question, as I am not sure how you SUM classes (are you talking about summing the total number of attendees?), is how often the data that makes up the sum will be changing/ how many places change it etc. If it is something that is run rarely, the data changes frequently and changes are made from all over the place then doing an on the fly calculation is probably the best bet. If however your running it frequently, its using a date as a group by, data is changed infrequently and only from a couple of stored procedures then creating a report table is most likely the best.