0

I am querying a database of hour entries and summing up by company and by week. I understand that MySQL's week function is based on a calendar week. That being said, I'm getting some unexpected grouping results. Perhaps you sharp-eyed folks can lend a hand:

SELECT * FROM (
    SELECT
        tms.date,
        SUM( IF( tms.skf_group = "HP Group", tms.hours, 0000.00 )) as HPHours,
        SUM( IF( tms.skf_group = "SKF Canada", tms.hours, 000.00 )) as SKFHours
    FROM time_management_system tms
    WHERE date >= "2012-01-01"
    AND date <= "2012-05-11" 
    AND tms.skf_group IN ( "HP Group", "SKF Canada" )
    GROUP BY WEEK( tms.date, 7 )
    # ORDER BY tms.date DESC
    # LIMIT 7
) AS T1
ORDER BY date ASC   

My results are as follows: (Occasionally we don't have entries on a Sunday for example. Do null values matter?)

('date'=>'2012-01-01','HPHours'=>'0.00','SKFHours'=>'2.50'),
('date'=>'2012-01-02','HPHours'=>'97.00','SKFHours'=>'78.75'),
('date'=>'2012-01-09','HPHours'=>'86.50','SKFHours'=>'100.00'),
('date'=>'2012-01-16','HPHours'=>'68.00','SKFHours'=>'96.25'),
('date'=>'2012-01-24','HPHours'=>'39.00','SKFHours'=>'99.50'),
('date'=>'2012-02-05','HPHours'=>'3.00','SKFHours'=>'93.00'),
('date'=>'2012-02-06','HPHours'=>'12.00','SKFHours'=>'122.50'),
('date'=>'2012-02-13','HPHours'=>'64.75','SKFHours'=>'117.50'),
('date'=>'2012-02-21','HPHours'=>'64.50','SKFHours'=>'93.00'),
('date'=>'2012-03-02','HPHours'=>'45.50','SKFHours'=>'143.25'),
('date'=>'2012-03-05','HPHours'=>'62.00','SKFHours'=>'136.75'),
('date'=>'2012-03-12','HPHours'=>'54.25','SKFHours'=>'133.00'),
('date'=>'2012-03-19','HPHours'=>'77.75','SKFHours'=>'130.75'),
('date'=>'2012-03-26','HPHours'=>'61.00','SKFHours'=>'147.00'),
('date'=>'2012-04-02','HPHours'=>'86.75','SKFHours'=>'96.75'),
('date'=>'2012-04-09','HPHours'=>'84.25','SKFHours'=>'120.50'),
('date'=>'2012-04-16','HPHours'=>'90.00','SKFHours'=>'127.25'),
('date'=>'2012-04-23','HPHours'=>'103.25','SKFHours'=>'89.50'),
('date'=>'2012-05-02','HPHours'=>'72.50','SKFHours'=>'143.75'),
('date'=>'2012-05-07','HPHours'=>'68.25','SKFHours'=>'119.00')

January 2nd is the first Monday, hence Jan 1st is only one day. I would expect the output to be consecutive Mondays (Monday Jan 2, 9, 16, 23, 30, etc)? The unexpected week groupings below continue throughout the results. Any ideas?

Thanks very much!

4

2 回答 2

3

It's not clear what selecting tms.date even means when you're grouping by some function on tms.date. My guess is that it means "the date value from any source row corresponding to this group". At that point, the output is entirely reasonable.

Given that any given group can have seven dates within it, what date do you want to get in the results?

EDIT: This behaviour is actually documented in "GROUP BY and HAVING with Hidden Columns":

MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause.
...
The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause. Sorting of the result set occurs after values have been chosen, and ORDER BY does not affect which values the server chooses.

The tms.date column isn't part of the GROUP BY clause - only a function operating on tms.date is part of the GROUP BY clause, so I believe the text above applies to the way that you're selecting tms.date: you're getting any date within that week.

If you want the earliest date, you might try

SELECT MIN(tms.date), ...

That's assuming that MIN works with date/time fields, of course. I can't easily tell from the documentation.

于 2012-05-11T20:46:07.647 回答
0

问题对我来说不清楚,但我想你不想按周分组。因为一周给出一年中的一周。今天是第 19 周。

我认为您想按工作日分组,例如 GROUP BY WEEKday(tms.date)

于 2012-05-11T20:49:57.350 回答