2013-02-19 79 views
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') 

OUTPUT:

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 
+2

你怎麼' 52'? – 2013-02-19 12:40:27

+1

爲什麼你分組後42? – 2013-02-19 12:40:34

回答

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 
0

根據您的期望的結果,我看到你給列TIMECARD.TIMECARD_IDTASK.MAN_HOURSMinimum最低)值和Sum)。 (但我爲什麼你的52價值沒有得到)

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 
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