如何通过此表的结果进行求和/分组

时间:2017-03-29 02:10:49

标签: php mysql select

我有一张小时表,看起来像是:

enter image description here

我想仅对本周的hours_spent结果求和,并按created_by人对结果进行分组。我有这个查询返回正确的数据,只显示本周的结果:

enter image description here

SELECT staff_id, first_name,  last_name, date_entered, `hours_spent` as total_hours FROM hours LEFT JOIN staff ON hours.created_by = staff.staff_id where yearweek(`date_entered`) = yearweek(curdate());

但是当我将SUM(hours_spent)添加为total_hours并按staff_id分组时,如下例所示,我得到0结果。

enter image description here

SELECT staff_id, date_entered, first_name, last_name, SUM(`hours_spent`) as total_hours FROM hours LEFT JOIN staff ON hours.created_by = staff.staff_id group by staff_id having yearweek(`date_entered`) = yearweek(curdate());

我假设它不起作用,因为我的声明的一部分没有返回单独的日期行,所以它会中断。

我觉得我这样做很难。我应该尝试对第一个查询的结果运行第二个求和查询,而不是将它们全部合并为一个(我希望清洁度)。或者我应该使用子查询来过滤掉本周不是的日期,然后将总数分组,如果是这样,我该如何实现?

1 个答案:

答案 0 :(得分:0)

我得到了我期待的东西:

SELECT staff.first_name,staff.last_name,sum(date_entered) 从几个小时 LEFT JOIN员工ON hours.created_by = staff.staff_id 在哪里年周(C:\Users\Primary User\Desktop\500lines-master\crawler\code>python test.py C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web.py:54: ResourceWarning: loop argument is deprecated warnings.warn("loop argument is deprecated", ResourceWarning) E.E..EEEEEEEEEE.. ====================================================================== ERROR: test_content_type (__main__.TestCrawler) ---------------------------------------------------------------------- Traceback (most recent call last): File "test.py", line 314, in test_content_type self.add_page(content_type='foo') File "test.py", line 93, in add_page self.add_handler(url, handler) File "test.py", line 76, in add_handler self.app.router.add_route('GET', url, handler) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route resource = self.add_resource(path, name=name) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource self.register_resource(resource) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource "Cannot register a resource into frozen router.") RuntimeError: Cannot register a resource into frozen router. ====================================================================== ERROR: test_encoding (__main__.TestCrawler) ---------------------------------------------------------------------- Traceback (most recent call last): File "test.py", line 309, in test_encoding test_charset('', 'utf-8') File "test.py", line 305, in test_charset self.add_page(url, content_type=content_type) File "test.py", line 93, in add_page self.add_handler(url, handler) File "test.py", line 76, in add_handler self.app.router.add_route('GET', url, handler) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route resource = self.add_resource(path, name=name) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource self.register_resource(resource) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource "Cannot register a resource into frozen router.") RuntimeError: Cannot register a resource into frozen router. ====================================================================== ERROR: test_link (__main__.TestCrawler) ---------------------------------------------------------------------- Traceback (most recent call last): File "test.py", line 126, in test_link self.add_page('/', ['/foo']) File "test.py", line 93, in add_page self.add_handler(url, handler) File "test.py", line 76, in add_handler self.app.router.add_route('GET', url, handler) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route resource = self.add_resource(path, name=name) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource self.register_resource(resource) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource "Cannot register a resource into frozen router.") RuntimeError: Cannot register a resource into frozen router. ====================================================================== ERROR: test_link_cycle (__main__.TestCrawler) ---------------------------------------------------------------------- Traceback (most recent call last): File "test.py", line 137, in test_link_cycle url = self.add_page('/foo', ['/bar']) File "test.py", line 93, in add_page self.add_handler(url, handler) File "test.py", line 76, in add_handler self.app.router.add_route('GET', url, handler) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route resource = self.add_resource(path, name=name) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource self.register_resource(resource) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource "Cannot register a resource into frozen router.") RuntimeError: Cannot register a resource into frozen router. ====================================================================== ERROR: test_max_tasks (__main__.TestCrawler) ---------------------------------------------------------------------- Traceback (most recent call last): File "test.py", line 264, in test_max_tasks self.add_handler(url, handler) File "test.py", line 76, in add_handler self.app.router.add_route('GET', url, handler) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route resource = self.add_resource(path, name=name) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource self.register_resource(resource) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource "Cannot register a resource into frozen router.") RuntimeError: Cannot register a resource into frozen router. ====================================================================== ERROR: test_max_tries (__main__.TestCrawler) ---------------------------------------------------------------------- Traceback (most recent call last): File "test.py", line 281, in test_max_tries self.add_handler('/', handler) File "test.py", line 76, in add_handler self.app.router.add_route('GET', url, handler) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route resource = self.add_resource(path, name=name) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource self.register_resource(resource) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource "Cannot register a resource into frozen router.") RuntimeError: Cannot register a resource into frozen router. ====================================================================== ERROR: test_non_html (__main__.TestCrawler) ---------------------------------------------------------------------- Traceback (most recent call last): File "test.py", line 322, in test_non_html self.add_page('/xml', body=body, content_type='application/xml') File "test.py", line 93, in add_page self.add_handler(url, handler) File "test.py", line 76, in add_handler self.app.router.add_route('GET', url, handler) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route resource = self.add_resource(path, name=name) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource self.register_resource(resource) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource "Cannot register a resource into frozen router.") RuntimeError: Cannot register a resource into frozen router. ====================================================================== ERROR: test_non_http (__main__.TestCrawler) ---------------------------------------------------------------------- Traceback (most recent call last): File "test.py", line 333, in test_non_http self.add_page(body=body) File "test.py", line 93, in add_page self.add_handler(url, handler) File "test.py", line 76, in add_handler self.app.router.add_route('GET', url, handler) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route resource = self.add_resource(path, name=name) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource self.register_resource(resource) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource "Cannot register a resource into frozen router.") RuntimeError: Cannot register a resource into frozen router. ====================================================================== ERROR: test_prohibited_host (__main__.TestCrawler) ---------------------------------------------------------------------- Traceback (most recent call last): File "test.py", line 152, in test_prohibited_host self.add_page('/', ['http://example.com']) File "test.py", line 93, in add_page self.add_handler(url, handler) File "test.py", line 76, in add_handler self.app.router.add_route('GET', url, handler) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route resource = self.add_resource(path, name=name) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource self.register_resource(resource) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource "Cannot register a resource into frozen router.") RuntimeError: Cannot register a resource into frozen router. ====================================================================== ERROR: test_redirect (__main__.TestCrawler) ---------------------------------------------------------------------- Traceback (most recent call last): File "test.py", line 196, in test_redirect url = self.add_redirect('/', foo) File "test.py", line 101, in add_redirect self.add_handler(url, handler) File "test.py", line 76, in add_handler self.app.router.add_route('GET', url, handler) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route resource = self.add_resource(path, name=name) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource self.register_resource(resource) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource "Cannot register a resource into frozen router.") RuntimeError: Cannot register a resource into frozen router. ====================================================================== ERROR: test_redirect_cycle (__main__.TestCrawler) ---------------------------------------------------------------------- Traceback (most recent call last): File "test.py", line 214, in test_redirect_cycle url = self.add_redirect('/bar', foo) File "test.py", line 101, in add_redirect self.add_handler(url, handler) File "test.py", line 76, in add_handler self.app.router.add_route('GET', url, handler) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route resource = self.add_resource(path, name=name) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource self.register_resource(resource) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource "Cannot register a resource into frozen router.") RuntimeError: Cannot register a resource into frozen router. ====================================================================== ERROR: test_redirect_join (__main__.TestCrawler) ---------------------------------------------------------------------- Traceback (most recent call last): File "test.py", line 230, in test_redirect_join self.add_redirect('/foo', baz) File "test.py", line 101, in add_redirect self.add_handler(url, handler) File "test.py", line 76, in add_handler self.app.router.add_route('GET', url, handler) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route resource = self.add_resource(path, name=name) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource self.register_resource(resource) File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource "Cannot register a resource into frozen router.") RuntimeError: Cannot register a resource into frozen router. ---------------------------------------------------------------------- Ran 17 tests in 0.034s FAILED (errors=12) C:\Users\Primary User\Desktop\500lines-master\crawler\code> ,1)=(年周(curdate(),1)-1) GROUP BY created_by