This is a discussion on Sum all sums within the MySQL Database forums, part of the Database Forums category; Consider the following table named "stats_dt". It stores an hourly summary of hits and sessions on the server: +------------+------+----------+------+ | ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Consider the following table named "stats_dt". It stores an hourly
summary of hits and sessions on the server: +------------+------+----------+------+ | date | hour | sessions | hits | +------------+------+----------+------+ | 2007-09-02 | 10 | 6 | 140 | +------------+------+----------+------+ | 2007-09-03 | 10 | 6 | 140 | +------------+------+----------+------+ | 2007-10-19 | 10 | 6 | 140 | +------------+------+----------+------+ | 2007-10-19 | 11 | 2 | 54 | +------------+------+----------+------+ | 2007-10-22 | 15 | 1 | 22 | +------------+------+----------+------+ | 2007-10-22 | 16 | 5 | 150 | +------------+------+----------+------+ | 2007-10-23 | 12 | 6 | 210 | +------------+------+----------+------+ | 2007-10-24 | 19 | 4 | 53 | +------------+------+----------+------+ | 2007-10-24 | 13 | 8 | 65 | +------------+------+----------+------+ | 2007-10-25 | 7 | 2 | 37 | +------------+------+----------+------+ | 2007-10-26 | 7 | 3 | 10 | +------------+------+----------+------+ | 2007-10-26 | 9 | 1 | 17 | +------------+------+----------+------+ I want to count the number of hits and sessions in the past week, so I wrote: SELECT `date`, SUM(hits), SUM(sessions) FROM stats_dt WHERE YEARWEEK(`date`) = YEARWEEK(CURDATE()) GROUP BY `date` DESC The resultset looks like: +------------+---------------+-----------+ | date | SUM(sessions) | SUM(hits) | +------------+---------------+-----------+ | 2007-10-22 | 6 | 172 | +------------+---------------+-----------+ | 2007-10-23 | 6 | 210 | +------------+---------------+-----------+ | 2007-10-24 | 12 | 118 | +------------+---------------+-----------+ | 2007-10-25 | 2 | 37 | +------------+---------------+-----------+ As expected, but not entirely what I want: I want to get the SUM of all hits and sessions, like this: +----------+------+ | sessions | hits | +----------+------+ | 26 | 537 | +----------+------+ I don't understand how to translate this into SQL. I tried this but MySQL returned a big error: SELECT SUM(SUM(hits)), SUM(SUM(sessions)) FROM stats_dt WHERE YEARWEEK(`date`) = YEARWEEK(CURDATE()) Could anybody help me out here? All help is appreciated :-) |
|
|||
|
dennis.sprengers@gmail.com wrote:
> I want to count the number of hits and sessions in the past week, so I > wrote: > > SELECT `date`, SUM(hits), SUM(sessions) > FROM stats_dt > WHERE YEARWEEK(`date`) = YEARWEEK(CURDATE()) > GROUP BY `date` DESC > > The resultset looks like: > > +------------+---------------+-----------+ > | date | SUM(sessions) | SUM(hits) | > +------------+---------------+-----------+ > | 2007-10-22 | 6 | 172 | > +------------+---------------+-----------+ > | 2007-10-23 | 6 | 210 | > +------------+---------------+-----------+ > | 2007-10-24 | 12 | 118 | > +------------+---------------+-----------+ > | 2007-10-25 | 2 | 37 | > +------------+---------------+-----------+ > > As expected, but not entirely what I want: I want to get the SUM of > all hits and sessions, like this: > > +----------+------+ > | sessions | hits | > +----------+------+ > | 26 | 537 | > +----------+------+ Don't group -- //Aho |