Sum all sums

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: +------------+------+----------+------+ | ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-27-2007
dennis.sprengers@gmail.com
 
Posts: n/a
Default Sum all sums

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 :-)

Reply With Quote
  #2 (permalink)  
Old 10-27-2007
J.O. Aho
 
Posts: n/a
Default Re: Sum all sums

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
Reply With Quote
  #3 (permalink)  
Old 10-27-2007
dennis.sprengers@gmail.com
 
Posts: n/a
Default Re: Sum all sums

Thanks!

* frown * stupid me...

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:25 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0