0

询问

SELECT 
    TIMECARD.EMP_ID, TIMECARD.TIMECARD_ID, TIMECARD.TIMECARD_STATUS,
    TASK.MAN_HOURS
FROM 
    TRN_TIMECARD_ACTIVITY ACT
INNER JOIN 
    TRN_TIMECARD TIMECARD ON TIMECARD.TIMECARD_ID = ACT.TIMECARD_ID
INNER JOIN 
    TRN_TIMECARD_TASK TASK ON TASK.TIMECARD_ID = TIMECARD.TIMECARD_ID 
WHERE 
    ACT.TIMECARD_STATUS IN ('AP','RE')

输出:

61  42  AP  9
61  42  AP  9
61  43  AP  9
61  43  AP  9
61  47  AP  7

我必须分组emp_id并计算sum(man_hours)

如何获得如下价值

61  42  AP  52
4

3 回答 3

1

...看不到中间两列在您的组中的数字,所以我会这样做:

SELECT        TIMECARD.EMP_ID, SUM(TASK.MAN_HOURS)
FROM          TRN_TIMECARD_ACTIVITY ACT
INNER JOIN    TRN_TIMECARD TIMECARD ON TIMECARD.TIMECARD_ID=ACT.TIMECARD_ID
INNER JOIN    TRN_TIMECARD_TASK TASK ON TASK.TIMECARD_ID=TIMECARD.TIMECARD_ID 
WHERE         ACT.TIMECARD_STATUS IN('AP','RE')
GROUP BY      TIMECARD.EMP_ID
于 2013-02-19T12:41:52.547 回答
0

根据您想要的结果,我看到您给出了列的Minimum最低)值TIMECARD.TIMECARD_ID和. (但我不明白为什么你会得到 52 的值SumTASK.MAN_HOURS

SELECT  TIMECARD.EMP_ID,
        MIN(TIMECARD.TIMECARD_ID) time_card,
        TIMECARD.TIMECARD_STATUS,
        SUM(TASK.MAN_HOURS) totalHours
FROM    TRN_TIMECARD_ACTIVITY ACT
        INNER JOIN TRN_TIMECARD TIMECARD ON TIMECARD.TIMECARD_ID=ACT.TIMECARD_ID
        INNER JOIN TRN_TIMECARD_TASK TASK ON TASK.TIMECARD_ID=TIMECARD.TIMECARD_ID 
WHERE   ACT.TIMECARD_STATUS IN('AP','RE')
GROUP   BY TIMECARD.EMP_ID, TIMECARD.TIMECARD_STATUS
于 2013-02-19T12:41:58.693 回答
0

我认为这会对您有所帮助...至少使用 TIMECARD.TIMECARD_ID

SELECT TIMECARD.EMP_ID,
       MIN(TIMECARD.TIMECARD_ID) TIMECARD_ID, 
       TIMECARD.TIMECARD_STATUS,
       SUM(TASK.MAN_HOURS) HOURS 
FROM TRN_TIMECARD_ACTIVITY ACT
INNER JOIN
TRN_TIMECARD TIMECARD ON TIMECARD.TIMECARD_ID=ACT.TIMECARD_ID
INNER JOIN
TRN_TIMECARD_TASK TASK ON TASK.TIMECARD_ID=TIMECARD.TIMECARD_ID 
WHERE ACT.TIMECARD_STATUS IN('AP','RE')
GROUP BY TIMECARD.EMP_ID,TIMECARD.TIMECARD_STATUS
于 2013-02-19T12:42:11.573 回答