MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 8th, 2012, 01:32 PM
estafford estafford is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 estafford User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 29 sec
Reputation Power: 0
[SOLVED] How to compare a "grand total" sum() to a group by sum?

I've got a testing table containing names and misc numbers. What i'm trying to do is;

1. get the sum of column_1 for each name within a date range
2. get the sum of column_2 for each name within a date range
3. get the percentage amount of column_1 against column_2 for each name within a date range.

and finally
4. get the total sum of column_1 for ALL names within a date range to calculate a percentage of column_1 for each name.

1-3 have been accomplished using 'Group By'. The problem I have now is, How do I include part 4, getting the grand total sum of all rows and not have it affected by 'Group By'? I've had no luck so far.

The working query for parts 1-3 is:

[MYSQL]
SELECT name,
SUM(column_1) as 'Attempt',
SUM(column_2) as 'Success',
(SUM(column_2) * 100) / SUM(column_1) as 'Percent_of_own'
FROM activelog WHERE column_1 > 0 AND
entrydate BETWEEN '2012/01/01' AND '2013/01/01'
GROUP BY name;
[/MYSQL]

This works fine. But now I want to somehow include something like:
(SUM(column_1) * 100) / << Grand Total>> AS 'Percent_of_all"

A sample or pointer to online resources, examples would be appreciated!

Last edited by estafford : November 9th, 2012 at 09:46 AM. Reason: Question Andwered, marked title as 'Solved'

Reply With Quote
  #2  
Old November 9th, 2012, 06:06 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,375 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 28 m 13 sec
Reputation Power: 4140
Code:
SELECT name
     , SUM(column_1) as 'Attempt'
     , SUM(column_2) as 'Success'
     , SUM(column_2) * 100.0 / SUM(column_1) as 'Percent_of_own'
     , SUM(column_1) * 100.0 / 
       ( SELECT SUM(column_1)
           FROM activelog 
          WHERE column_1 > 0 
            AND entrydate BETWEEN '2012/01/01' 
                              AND '2013/01/01' ) AS 'Percent_of_all'
  FROM activelog 
 WHERE column_1 > 0 
   AND entrydate BETWEEN '2012/01/01' 
                     AND '2013/01/01'
GROUP 
    BY name
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old November 9th, 2012, 09:43 AM
estafford estafford is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 estafford User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 29 sec
Reputation Power: 0
Thumbs up Thank You !!

Quote:
Originally Posted by r937
Code:
SELECT name
     , SUM(column_1) as 'Attempt'
     , SUM(column_2) as 'Success'
     , SUM(column_2) * 100.0 / SUM(column_1) as 'Percent_of_own'
     , SUM(column_1) * 100.0 / 
       ( SELECT SUM(column_1)
           FROM activelog 
          WHERE column_1 > 0 
            AND entrydate BETWEEN '2012/01/01' 
                              AND '2013/01/01' ) AS 'Percent_of_all'
  FROM activelog 
 WHERE column_1 > 0 
   AND entrydate BETWEEN '2012/01/01' 
                     AND '2013/01/01'
GROUP 
    BY name


@r937
Brilliant! Thank you. Your example is super clean and easy to follow. I came close in one of my attempts, but had one of the parens in the wrong place - Select(SUM... instead of (SELECT SUM ...

Thanks again for your help!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > How to compare a "grand total" sum() to a group by sum?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap