Forum Moderators: open

Message Too Old, No Replies

SUM or CASE?

trying to have multiple GROUP By statements

         

tonynoriega

6:55 pm on Aug 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i know im bombarding the forums and i apologize, but i have a very important feature i am trying to add to my site, which will help me tremendously....as well as get some people off my back....

i have been reading on Pivot Tables, INNER JOINS, SUM, CASE statements in regards to having multiple GROUP BY statements in my query string.

here is what i have currently:

$sql = 'SELECT nhm_associate, COUNT(nhm_associate) AS weekscount,(SELECT COUNT(*) FROM registration_table WHERE DATE_SUB(CURDATE(),INTERVAL 6 DAY) <= entry_time) AS totalcount FROM registration_table WHERE DATE_SUB(CURDATE(),INTERVAL 6 DAY) <= entry_time GROUP BY nhm_associate ORDER BY entry_time';

i also want to add this:

SELECT COUNT(*) FROM registration_table WHERE DATE_SUB(CURDATE(),INTERVAL 2 DAY) = entry_time AS yesterday GROUP BY nhm_associate

i would be tremendously grateful for any insight on how to combine these statements...

tonynoriega

7:22 pm on Aug 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, i have come across an example for a SUM CASE statement that should work, but am not able to get it to query correctly....example below...

cant get this to work quite right, but i know i am close...

SELECT nhm_associate, COUNT(nhm_associate) WHERE DATE_SUB(CURDATE(),INTERVAL 6 DAY) <= entry_time AS weekscount,
SUM(CASE COUNT(*) WHERE DATE_SUB(CURDATE(),INTERVAL 6 DAY) <= entry_time THEN 1 END) AS totalcount,
SUM(CASE COUNT(*) WHERE DATE_SUB(CURDATE(),INTERVAL 2 DAY) = entry_time THEN 1 END) AS yesterday
FROM registration_table GROUP BY nhm_associate';