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

10-20-04, 10:56
|
|
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
|
|

10-20-04, 11:33
|
|
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
|
|

10-20-04, 11:58
|
|
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!
|
|

10-20-04, 11:58
|
|
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!
|
|

10-20-04, 12:04
|
|
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!
|
|

10-20-04, 12:05
|
|
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!
|
|

10-20-04, 20:02
|
|
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
|
|

10-20-04, 23:01
|
|
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)
|
|

10-21-04, 06:04
|
|
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
|
|

10-21-04, 12:12
|
|
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;
|
|

11-05-04, 08:24
|
|
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!
|
|

11-05-04, 08:52
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
|
|
add having value_tot > 0 to the subquery
|
|

11-05-04, 10:03
|
|
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!
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|