如何对另一列中每个不同值的列值求和

时间:2014-01-21 08:49:01

标签: mysql

我一直致力于根据人员和工作站的工作时间进行绩效评估报告。我设法从我需要的表中获取所有信息但我无法最终确定代码以使其显示为单行。这是我用的;

首先我使用这两个选项,这样我只有我需要的东西,因为这两个表都有超过20-25列和很多行;

SELECT CONFIRMATION, CONFIRMPOS, POTYPE, PRDORDER,
     OPERATION,PERSONELNUM,PERSONELNAME 
    FROM PERSONNEL 
    INTO TMPPERSONEL;

SELECT WORKCENTER, CONFIRMATION, CONFIRMPOS,SETUP,
     MACHINE, LABOUR, CONFIRMDATE 
    FROM CONF 
    INTO TMPCONF;

接下来,我使用此选项将两个表组合起来进行处理;

SELECT TMPPERSONNEL.POTYPE, TMPPERSONNEL.PRDORDER, TMPPERSONNEL.OPERATION, TMPCONF.WORKCENTER, TMPCONF.CONFIRMATION, TMPCONF.CONFIRMPOS, TMPPERSONNEL.PERSONELNUM, TMPPERSONNEL.PERSONELNAME, TMPCONF.SETUP, TMPCONF.MACHINE, TMPCONF.LABOUR, TMPCONF.CONFIRMDATE 
    FROM TMPPERSONNEL  LEFT JOIN TMPCONF ON  TMPCONF.CONFIRMATION = TMPPERSONNEL.CONFIRMATION 
        AND TMPCONF.CONFIRMPOS = TMPPERSONNEL.CONFIRMPOS 
    INTO TMPREPORT;

现在我要做的是在特定日期(DATE1,DATE2)之间为每个人或工作站计算工作时间(SETUP,MACHINE,LABOR)并将其显示为单行。例如,从这个表

PERSONELNUM  PERSONELNAME  WORKCENTER   SETUP  MACHINE  LABOUR  CONFIRMDATE 
1             Personnel1   WORKCENTER1     5     20       20    01.01.2014
1             Personnel2   WORKCENTER1     5     20       20    01.01.2014
1             Personnel2   WORKCENTER3     5     20       20    02.01.2014
1             Personnel1   WORKCENTER2     5     20       20    02.01.2014
1             Personnel3   WORKCENTER1     5     20       20    02.01.2014
1             Personnel3   WORKCENTER2     5     20       20    03.01.2014
1             Personnel1   WORKCENTER1     5     20       20    03.01.2014
1             Personnel2   WORKCENTER3     5     20       20    04.01.2014
1             Personnel1   WORKCENTER2     5     20       20    04.01.2014
1             Personnel3   WORKCENTER1     5     20       20    04.01.2014

PERSONELNUM  PERSONELNAME  SETUP  MACHINE  LABOUR 
1             Personnel1     20     80       80  
2             Personnel2     15     60       60    
3             Personnel3     15     60       60   

如果我的解释令人困惑,我很抱歉。因为我是初学者,所以我只能用我的小知识来描述问题。

编辑:如果我想看到这样的话,我该怎么办:

PERSONELNUM  PERSONELNAME  WORKCENTER    SETUP  MACHINE  LABOUR 
  1          Personnel1    WORKCENTER1     10     40       40  
  1          Personnel1    WORKCENTER2     10     40       40 
  1          Personnel2    WORKCENTER1      5     20       20      
  1          Personnel2    WORKCENTER3     10     40       40 
  1          Personnel3    WORKCENTER1     10     40       40 
  1          Personnel3    WORKCENTER2      5     20       20  

3 个答案:

答案 0 :(得分:0)

您的问题似乎与this one非常相似?

我在

下面列出了一个可能的解决方案
SELECT PERSONELNUM, PERSONELNAME, SUM(SETUP),SUM(MACHINE),SUM(LABOUR) AS PERSONELNAME_SUM FROM TABLE GROUP BY PERSONELNAME

答案 1 :(得分:0)

这样的事情应该有效。

SELECT
 PERSONALNUM,
 PERSONALNAME,
 SUM(SETUP) AS SETUP,
 SUM(MACHINE) AS MACHINE,
 SUM(LABOUR) AS LABOUR
FROM
 TMPREPORT
WHERE
 CONFIRMDATE BETWEEN 'DATE1' AND 'DATE2'
GROUP BY
 PERSONELNUM;

请参阅http://dev.mysql.com/doc/refman/5.5/en/group-by-functions.html

答案 2 :(得分:0)

您是否尝试过对personelnum列进行分组并对其他列进行求和? 类似的东西:

SELECT p.POTYPE, p.PRDORDER, p.OPERATION, c.WORKCENTER, c.CONFIRMATION, c.CONFIRMPOS, p.PERSONELNUM, p.PERSONELNAME, sum(c.SETUP), sum(c.MACHINE), sum(c.LABOUR), c.CONFIRMDATE 
FROM TMPPERSONNEL as p LEFT JOIN TMPCONF as c ON c.CONFIRMATION = p.CONFIRMATION 
    AND c.CONFIRMPOS = p.CONFIRMPOS 
group by
    p.PERSONELNUM
 INTO TMPREPORT;

要按列添加多个组,您必须使用逗号分隔每个组

SELECT p.POTYPE, p.PRDORDER, p.OPERATION, c.WORKCENTER, c.CONFIRMATION, c.CONFIRMPOS, p.PERSONELNUM, p.PERSONELNAME, sum(c.SETUP), sum(c.MACHINE), sum(c.LABOUR), c.CONFIRMDATE 
FROM TMPPERSONNEL as p LEFT JOIN TMPCONF as c ON c.CONFIRMATION = p.CONFIRMATION 
    AND c.CONFIRMPOS = p.CONFIRMPOS 
group by
    p.PERSONELNUM, c.WORKCENTER
 INTO TMPREPORT;