If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > average

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-20-04, 10:56
diegolaz diegolaz is offline
Registered User
 
Join Date: Feb 2004
Location: Argentina
Posts: 54
average

Hi, I have a DB with entries by several users with a date and value. Can I get the average per user per month in 1 query?
I'm using

SELECT id_usuario,AVG( horas_t )
FROM (SELECT id_usuario,(SUM( horas_planta + horas_bios )/12) AS horas_t FROM Horas
GROUP BY id_usuario) AS TProm
GROUP BY id_usuario

The problem is that some users started later and have 0 in some months.... how can I discriminate this in the same query

Thanks!
PS: using PHP 4.3.8 and MySQL 4.1.3b-beta
Reply With Quote
  #2 (permalink)  
Old 10-20-04, 11:33
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
Hello,

Just add a HAVING clause to your GROUP BY :

Code:
SELECT id_usuario,AVG( horas_t )
FROM (SELECT id_usuario,(SUM( horas_planta + horas_bios )/12) AS horas_t FROM Horas
GROUP BY id_usuario) AS TProm
GROUP BY id_usuario
HAVING AVG( horas_t ) > 0
Regards,

RBARAER
Reply With Quote
  #3 (permalink)  
Old 10-20-04, 11:58
diegolaz diegolaz is offline
Registered User
 
Join Date: Feb 2004
Location: Argentina
Posts: 54
nop.....

thanks, but that is not the problem... the thing is that I don't want to divide by 12 but for the month he realy worked...... if he startes on month 8 divide his year by 4......
mmmm have i mixed you up?
thanks!
Reply With Quote
  #4 (permalink)  
Old 10-20-04, 11:58
diegolaz diegolaz is offline
Registered User
 
Join Date: Feb 2004
Location: Argentina
Posts: 54
nop.....

thanks, but that is not the problem... the thing is that I don't want to divide by 12 but for the month he realy worked...... if he startes on month 8 divide his year by 4......
mmmm have i mixed you up?
thanks!
Reply With Quote
  #5 (permalink)  
Old 10-20-04, 12:04
diegolaz diegolaz is offline
Registered User
 
Join Date: Feb 2004
Location: Argentina
Posts: 54
nop....

thanks, but that is not the problem... the thing is that I don't want to divide by 12 but for the month he realy worked...... if he startes on month 8 divide his year by 4......
mmmm have i mixed you up?
thanks!
Reply With Quote
  #6 (permalink)  
Old 10-20-04, 12:05
diegolaz diegolaz is offline
Registered User
 
Join Date: Feb 2004
Location: Argentina
Posts: 54
nop....

thanks, but that is not the problem... the thing is that I don't want to divide by 12 but for the month he realy worked...... if he startes on month 8 divide his year by 4......
mmmm have i mixed you up?
thanks!
Reply With Quote
  #7 (permalink)  
Old 10-20-04, 20:02
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
select id_usuario
     , sum( horas_planta + horas_bios )
      /count(*) as horas_t 
  from horas 
group 
    by id_usuario
the above query assumes that there aren't any actual rows with zeros in them

i mean, if he didn't start until september, you're not entering zero rows for january through august, are you?

therefor he will have only 4 rows, so COUNT(*) will be 4
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 10-20-04, 23:01
diegolaz diegolaz is offline
Registered User
 
Join Date: Feb 2004
Location: Argentina
Posts: 54
yes and no

yes, you are right...... there are only entries on the dates specified. But the entrys are made every day (1 record per day, per user) and I wanted to make a an avarage per month..... agrouping all record by month......
sorry for not explaning better before.
And I think I will just make a for cicle from month 1 to 12....
thanks to all responses! and sorry for that repeated messege (my connection was working bad)
Reply With Quote
  #9 (permalink)  
Old 10-21-04, 06:04
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
If you want to display the total per user per month, provided date_field is your date column (sorry but I don't quite understand the meaning of your columns ) , try :
Code:
select id_usuario, DATE_FORMAT(date_field, '%b') as month, sum( horas_planta + horas_bios ) as horas_t 
  from horas 
group 
    by id_usuario, DATE_FORMAT(date_field, '%b');
And if you want the average per user on a month :
Code:
select id_usuario, avg(horas_t) from
(
select id_usuario, DATE_FORMAT(date_field, '%b') as month, sum( horas_planta + horas_bios ) as horas_t 
  from horas 
group 
    by id_usuario, DATE_FORMAT(date_field, '%b')
)
group by id_usuario;
I can't test them right now, but I think these should work.

Best regards,

RBARAER
Reply With Quote
  #10 (permalink)  
Old 10-21-04, 12:12
diegolaz diegolaz is offline
Registered User
 
Join Date: Feb 2004
Location: Argentina
Posts: 54
Excelent!

The only thing missing was the subquery table ALIAS..... but it worked perfectly!! Thanks!!!
This is the resulting code:

CODE:
SELECT id_user, avg(value_tot)
FROM ( SELECT id_user, DATE_FORMAT(date_field, '%b') as month,
sum(value_1 + value_2 ) as value_tot
FROM horas
GROUP BY id_user, DATE_FORMAT(date_field, '%b')
) AS TTemp
GROUP BY id_user;
Reply With Quote
  #11 (permalink)  
Old 11-05-04, 08:24
diegolaz diegolaz is offline
Registered User
 
Join Date: Feb 2004
Location: Argentina
Posts: 54
a little problem...

Quote:
Originally Posted by diegolaz
Code:
SELECT id_user, avg(value_tot) 
FROM ( SELECT id_user, DATE_FORMAT(date_field, '%b') as month, 
                          sum(value_1 + value_2 ) as value_tot
            FROM horas 
            GROUP BY id_user, DATE_FORMAT(date_field, '%b')
           ) AS TTemp
GROUP BY id_user;
Changed DATE_FORMAT(date_field, '%b') by DATE_FORMAT(date_field, '%c')

I have a little problem, with some user having an entry in their firs month with value 0. These throws de average down a little... is there a way to discriminate these month if its not > 0 ??
Thanks!
Reply With Quote
  #12 (permalink)  
Old 11-05-04, 08:52
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
add having value_tot > 0 to the subquery
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #13 (permalink)  
Old 11-05-04, 10:03
diegolaz diegolaz is offline
Registered User
 
Join Date: Feb 2004
Location: Argentina
Posts: 54
yes!

Excelent! It worked fine!.... it wasn't that difficult I supose.... but sometimes you get blocked

Thanks a lot!
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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On