ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: RH 7.3/8.0/9.0, Debian Stable 3.0, FreeBSD 5.2, Solaris 8/9/10,HP-UX
Posts: 340
Rep:
SQL question - total bytes per month
I have a software which monitors the number of bytes downloaded/uploaded. The value is updated every 60 seconds so that it can be graphed. A typical table would be:
,where 'totalbytes' depicts the total number of bytes since the very first time the software was used, and 'bytes' is the number of bytes downloaded for that monitoring period (of 60 seconds).
My aim is that everytime i log in, i know the total value of bytes i've downloaded during that month - I'm trying to do it in two ways:
(1) I subtract the 'totalbytes' value at the 1st day of the month from the current 'totalbytes' value. This is ok but has a big downside that it excludes the downloaded value of the first day (arithmetic-wise)
or
(2) I select all the 'bytes' values and add them up for that month. This works fine. But this incurs more load since I have to add thousands of values (one value for every minute, thus, for one month of 31 days, I have to add 44,640 values, which I'm trying to avoid!!)
My question is whether there is some SQL syntax which i'm missing, which could be used to tackle my somehow-looking-simple-problem in an efficient manner - i.e. knowing the totalbytes (which is an always cumulative value) since the first time i used the program, how can i get the number of bytes downloaded from the 1st day of that month till the date of making the query?
Distribution: RH 7.3/8.0/9.0, Debian Stable 3.0, FreeBSD 5.2, Solaris 8/9/10,HP-UX
Posts: 340
Original Poster
Rep:
the only way i had to resolve it was by summing the column for one particular month. I couldn't do it the way mfeat proposed, because, as i have explained, the month's length is not always the same (varying from 28 to 31 days). So effectivly, i had to perform some 40,000 additions.
If the sql server you are using does not have a function to get the the last day of the month you could use the decode function (assuming it has one) as in decode(month,1,31,2,28,3,31...12,31), of course leap years are a problem.
I don't know how you're getting the totalbytes for a given day, are you using a max function on the time to get the latest record for a specified day? Show the sql code that you are using, that would help a lot.
Distribution: RH 7.3/8.0/9.0, Debian Stable 3.0, FreeBSD 5.2, Solaris 8/9/10,HP-UX
Posts: 340
Original Poster
Rep:
the totalbytes for a given day and the cumulative totalbytes are generated by means of a metering software which is niterfaced to the mysql server. I don't know, and have no idea how to find out, whether mysql has the ability to get the last day of the month - if it has that ability, it would save a lot of hassle
No need to know about the interface that loads the data, what I was asking for is the SQL code you are using to generate the report showing the time used for the month.
The following link shows documentation for the last_day function for MySQL:
LAST_DAY(date)
Takes a date or datetime value and returns the corresponding value for the last day of the month. Returns NULL if the argument is invalid.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.