Database Daily Totals

Hey,

I am creating a database that keeps track of shipment weights. They are time stamped and archived for one month.

How would I go about making another database table, or a function that can find the totals per day for a monthly graph of shipments?

The database stores many shipments per day.

Thanks

Well, lucky for you this is what databases are born to do. The SQL language (which is generally uniform but differs a bit from vendor to vendor) has what are called Aggregate Functions, such as MAX, MIN, and most relevant to you, SUM.

If I did a query like thisā€¦

SELECT SUM(weight) FROM weight_record

Iā€™d get back the sum of the weights for all time.

But, you want to break it up by day. Thatā€™s what SQLā€™s ā€œGROUP BYā€ clause is for. It lets you break up the datasets that aggregate functions apply to.

For the sake of simplicity, imagine we had a column in that weight table called ā€œdayā€, which had the day of the month for that record in it. We could do the followingā€¦

SELECT day, SUM(weight) FROM weight_record GROUP BY day

and end up with the sum for each day. Trouble is, you probably donā€™t have that column. Instead, each row has a timestamp. So, what you need to do is find a way to use just the date portion of the whole time.

How you do that will depend on the database system youā€™re using, since youā€™ll be using a function provided by the db system instead of the sql language. Letā€™s assume youā€™re using MySQL. Going over to the MySQL Date/Time Function Reference Page we find thereā€™s a function called Date that will extract just the date from a timestamp.

So, using that, we end up with:

SELECT DATE(t_stamp), SUM(weight) FROM weight_record GROUP BY DATE(t_stamp)

and VOILA, you end up with the weights by day. Clean it up a bit (mainly sort the data), and itā€™s ready to go in the graph:

SELECT DATE(t_stamp) as 'dt', SUM(weight) FROM weight_record GROUP BY dt ORDER BY dt ASC

(Note: I also gave the column an ALIAS to make things a bit easier to read and work with).

Hope that helps. Basically, I wanted to show you how to do it, but also give you some vocab as a starting point for looking up more about SQL on your own. Main points: aggregate functions, group by clause, and the order by clause.

Regards,

1 Like

Thank You very much!

Iā€™ll give it a shot.

Related Question,

What would go under the WHERE if you wanted to only look between certain dates?

WHERE dt>= Startdate AND dt<= EndDate?

We are using FSQL

Yes, you could do something like that, though you could also just use the t_stamp column. Irregardless of what columns youā€™re retrieving, you can use any available columns in your where clause.

Why would you choose one over the other? Basically the answer comes down to performance. The t_stamp column is indexed, meaning the db can (theoretically) locate a particular value in less time than it would take to look through the whole table. If you used ā€˜dtā€™ in your where clause, the db would first need to calculate what that means for each row, and then narrow it down to what you wanted. By using timestamp, it can quickly just grab the matching rows, and then only calculate dt for those.

Ultimately in your case Iā€™d doubt youā€™d notice the difference, so Iā€™d go with whatā€™s most readableā€¦ but I just wanted to give you some info that might come in handy later.

Oh, and yeah, your WHERE clause looks good, as long as ā€˜StartDateā€™ and ā€˜EndDateā€™ are properly formatted dates.

Regards,