当按多列分组时,MySQL查询隐藏结果

时间:2014-12-19 15:11:55

标签: mysql

我试图让按县分列的1月到11月的员工工作时间缩短。我只对过去60天在任何地方工作的员工感兴趣。

当我运行此查询时:

SELECT DISTINCT(a.empid) AS EMPL_ID, a.cnty AS COUNTY, SUM(a.hours) AS HRS 
FROM timesheet_lines AS a
WHERE a.date >= '2014-1-1'
AND a.date <= '2014-11-30'
GROUP BY EMPL_ID
HAVING MAX(a.date) > '2014-10-18';

我得到了正确的小时数,但没有按县分解。

当我按照此查询添加按县分组时:

SELECT DISTINCT(a.empid) AS EMPL_ID, a.cnty AS COUNTY, SUM(a.hours) AS HRS 
FROM timesheet_lines AS a
WHERE a.date >= '2014-1-1'
AND a.date <= '2014-11-30'
GROUP BY EMPL_ID, COUNTY
HAVING MAX(a.date) > '2014-10-18';

它按县划分,但它删除了那些在过去60天内没有在该特定县工作的员工在县里工作的时间。

如何按县添加细分,仍然可以获得所有工作时间?

以下是我想为一名员工提取的数据示例:

EMPL_ID COUNTY  HRS
------- ------  ---
5461    ONTARIO 105
5461    WAYNE   1182
5461    YATES   24

以下是我仅通过EMPL_ID分组时得到的内容:

EMPL_ID COUNTY  HRS
------- ------  ---
5461    ONTARIO 1311

具有正确的总小时数,但它没有被COUNTY正确分解。

以下是我按照EMPL_ID 县分组时得到的内容:

EMPL_ID COUNTY  HRS
------- ------  ---
5461    ONTARIO 105
5461    WAYNE   1182

现在它被COUNTY分解了,但它忽略了我认为的YATES县,因为该员工在过去60天内没有在YATES县工作。

2 个答案:

答案 0 :(得分:0)

首先,当您使用distinct时,您不需要group by

其次,问题是你的having条款。您需要过滤员工,但报告员工/县组合。这需要某种子查询。这是一种方法:

SELECT a.empid AS EMPL_ID, a.cnty AS COUNTY, SUM(a.hours) AS HRS 
FROM timesheet_lines AS a
WHERE a.date >= '2014-01-01' AND a.date <= '2014-11-30' AND
      a.empid IN (SELECT EmpId
                  FROM timesheet_lines tl
                  GROUP BY EmpId
                  HAVING MAX(date) > '2014-10-18'
                 )
GROUP BY EMPL_ID, COUNTY;

EDIT;

如果in花费的时间过长,请将其替换为join

SELECT a.empid AS EMPL_ID, a.cnty AS COUNTY, SUM(a.hours) AS HRS 
FROM timesheet_lines a JOIN
      (SELECT EmpId
       FROM timesheet_lines tl
       GROUP BY EmpId
       HAVING MAX(date) > '2014-10-18'
      ) e
      on a.EmpId = e.EmpId
WHERE a.date >= '2014-01-01' AND a.date <= '2014-11-30'
GROUP BY EMPL_ID, COUNTY;

实际上,我应该以这种方式开始编写,但我没有意识到你的数据有多大(我认为IN版本更容易阅读)。

答案 1 :(得分:0)

这是我最终得到的结果,并且效果很好:

CREATE TEMPORARY TABLE tsltemp 
SELECT empid
FROM timesheet_lines tl
GROUP BY empid
HAVING MAX(date) > '2014-10-18';


SELECT a.empid AS EMPL_ID, a.cnty AS COUNTY, SUM(a.hours) AS HRS 
FROM timesheet_lines AS a
WHERE a.date >= '2014-01-01' AND a.date <= '2014-11-30'
  AND a.empid IN (SELECT empid FROM tsltemp)
GROUP BY EMPL_ID, COUNTY;

这速度惊人得快。感谢Gordon,让我走上正轨!