My question about My SQL is,
I have a table where users enter time reporting data called time_report. Each time_report is entered with a service code, each of which has a different meaning and includes a start, stop, time and total.
Here is a example of data entered in the time_report table.
t_id Date User Customer Service Closing System Part Start Stop Total 53 2011-05-02 13 0 48 4 0 0 09:00 17.15 8.15 54 2011-05-03 13 0 49 4 0 0 09:00 17:00 8 55 2011-05-04 13 0 48 4 0 0 09:00 17.15 8:15 61 2011-05-04 1 0 52 4 0 0 09:00 17.15 8:15 62 2011-05-05 1 0 48 4 0 0 09:00 17.15 8:15
I am trying to run a query to extract 3 pieces of information.
user ( the user is the fo开发者_如何学Creign key to users.user_id) sum(total) per user. sun(total) per user where service < 49 Utilization Percentage: (This will be calculated by dividing the 2 totals )
Service is the foreign key to servicecodes.s_id
When I try and run the query to get the sum(total it work okay but I cannot group the information together
mysql> SELECT Users.full_name,sum(total) -> FROM time_report, users -> WHERE time_report.User = users.user_id -> AND date -> BETWEEN '2011-0502' -> AND '2011-05-11' -> GROUP BY User;
+-----------------+------------+ | full_name | sum(total) | +-----------------+------------+ | Cian Higgins | 26 | | Wallace Ward | 23 | | jason ward | 42 | | Thomas Woods | 72 | | Peter Jones | 49 | | fintan corrigan | 40 | | David Jones | 35 | | January Jones | 23 | | Joe Johnson | 24 | +-----------------+------------+ 9 rows in set, 1 warning (0.09)
When I run the query with the service < 49
SELECT Users.full_name,sum(total) AS Productive FROM time_report, users WHERE time_report.User = users.user_id AND date BETWEEN '2011-0502' AND '2011-05-11' AND Service < 49 GROUP BY User; +-----------------+------------+ | full_name | Productive | +-----------------+------------+ | Cian Higgins | 14 | | Wallace Ward | 23 | | jason ward | 33 | | Thomas Woods | 53 | | Peter Jones | 41 | | fintan corrigan | 32 | | David Jones | 27 | | January Jones | 23 | | Joe Johnson | 24 | +-----------------+------------+ 9 rows in set, 1 warning (0.03 sec)
But If I try and join the reports, I get errors....also if I try and run it as a subquery i get errors
Example:
SELECT Users.full_name, sum( total ) FROM time_report, users WHERE time_report.User = users.user_id AND date BETWEEN '2011-0502' AND '2011-05-11' GROUP BY User UNION SELECT Users.full_name, sum( total ) AS Productive FROM time_report, users WHERE time_report.User = users.user_id AND date BETWEEN '2011-0502' AND '2011-05-11' AND Service <49 GROUP BY User
this gives all the results in the one row
full_name sum(total) Cian Higgins 26 Wallace Ward 23 jason ward 42 Thomas Woods 72 Peter Jones 49 fintan corrigan 40 David Jones 35 January Jones 23 Joe Johnson 24 Cian Higgins 14 jason ward 33 Thomas Woods 53 Peter Jones 41 fintan corrigan 32 David Jones 27
You could use a CASE statement to refine the sum and have both sums returned with one statement.
SELECT Users.full_name
,sum(total)
,sum(case when service < 49 then total else 0 end) AS Productive
FROM time_report
, users
WHERE time_report.User = users.user_id
AND date BETWEEN '2011-0502' AND '2011-05-11'
GROUP BY
User
精彩评论