假設我有一個表中包含用戶數據,例如:如何在PL彙總/ SQL
userID calltime result
1 10:20 1
1 11:00 2
1 11:30 1
2 9:30 1
2 11:00 1
3 10:00 1
3 10:30 2
3 11:00 1
3 11:30 2
現在我想總結一下這樣的:
userID result1 result2
1 2 1
2 2 0
3 2 2
(total) 6 3
對我來說,彙總用戶數據沒問題,但是如何在查詢中添加總行? UNION不適用於此。
非常感謝你。
編輯:這是我當前的查詢,當然,這是行不通的:
PROCEDURE P_SUMMARIZE_CALL
(
pStartDate IN DATE,
pEndDate IN DATE,
SummaryCur OUT MEGAGREEN_CUR
)
IS
BEGIN
OPEN SUMMARYCUR FOR
SELECT USERID, TOTALCALLS,CONNECTEDCALLS,RATE,NOANSWER FROM
(((SELECT USERID,
count(CALLID) AS TOTALCALLS,
sum(CONNECTED) as CONNECTEDCALLS,
sum(CONNECTED)/count(CALLID)*100 || '%' AS RATE,
(count(CALLID) - sum(CONNECTED)) AS NOANSWER
FROM CALLLOGS
WHERE STARTTIME BETWEEN pStartDate AND pEndDate
group by USERID) c
FULL OUTER JOIN USERS u
ON c.USERID = u.ID)
UNION ALL
(SELECT NULL,count(CALLID) AS TOTALCALLS,
sum(CONNECTED) as CONNECTEDCALLS,
sum(CONNECTED)/count(CALLID)*100 || '%' AS RATE,
(count(CALLID) - sum(CONNECTED)) AS NOANSWER
FROM CALLLOGS
WHERE STARTTIME BETWEEN pStartDate AND pEndDate group by NULL));
END;
你有什麼理由使用'完全外部連接'嗎?這可能會做更多的工作,因爲看起來你實際上想要一個'RIGHT OUTER JOIN',除非我錯了。它會帶回很多完全空行,因爲你使用的是c.USERID而不是我想要的u.ID – 2010-11-22 13:53:48
。當在gridcontrol中顯示時,null值將被轉換爲0,如果我不使用RIGHT OUTER JOIN,沒有調用的用戶將被忽略 – Vimvq1987 2010-11-22 15:18:11