2012-08-16 53 views
2

我在SQL中很初學。我只能單獨做所有這些,但當我必須在相同的SQL命令中執行所有這些操作時,我感到困惑。所以,請你好心幫我在這..SQL:如何在同一個sql命令中使用count,sum和max函數用於MS ACCESS

我有3個表tbl_user,tbl_sActivity,tbl_client

tbl_user 
    user_id | first_name | last_name | 
    1  | name1  | last1 | 
    21  | name2  | last2 | 
    22  | name3  | last3 | 

tbl_sActivity 
user_id | client_id | act_status | act_date | 
1  | 7  | cold | 19/7/12 | 
1  | 7  | dealed | 30/7/12 | 
22  | 5  | cold | 7/8/12 | 
1  | 6  | cold | 16/7/12 | 
1  | 6  | warm | 18/7/12 | 
1  | 6  |  hot | 25/7/12 | 
1  | 6  | dealed | 7/8/12 | 
1  | 8  | warm | 1/8/12 | 
1  | 8  | dealed | 12/8/12 | 
21  | 4  | warm | 8/8/12 | 
21  | 4  | dealed | 10/8/12 | 

tbl_client 
client_id | client_price | 
4   |  400  | 
5   |  500  | 
6   |  600  | 
7   |  700  | 
8   |  800  | 

我想要的輸出看起來像這樣

user_id |Count_Client_id |Sum_client_price | Max_act_date| 
1  |  3   |  2100  | 12/8/12 | 
21  |  1   |  400  | 10/8/12 | 
22  |  1   |  500  | 7/8/12 | 

非常感謝你下面是很多 是我的嘗試...

SELECT tbl_user.user_id, Count(tbl_client.client_id) AS count_client, 
     Sum(tbl_client.client_price) AS SumOfclient_price, tbl_user.first_name, 
     tbl_user.last_name, Max(tbl_sActivity.act_date) AS MaxOfact_date 
FROM (tbl_client INNER JOIN tbl_sActivity ON 
     (tbl_client.client_id=tbl_sActivity.client_id) AND 
     (tbl_sActivity.client_id=tbl_client.client_id)) INNER JOIN tbl_user ON 
     tbl_sActivity.user_id=tbl_user.user_id 
GROUP BY tbl_user.user_id, tbl_user.first_name, tbl_user.last_name; 

這是返回正確的輸出

SELECT tmp.user_id, Count(*) AS count_distinct_clients 
FROM (SELECT DISTINCT user_id, client_id FROM tbl_sActivity) AS tmp 
GROUP BY tmp.user_id; 
+1

你能後你試一下查詢?將幫助我們知道從哪裏開始幫助您找到正確的解決方案。 – 2012-08-16 02:57:33

+0

+1,我abt寫這個查詢,但想看看做了什麼。可以做作業嗎? ;) – Abhinav 2012-08-16 03:02:57

+0

你確定你的結果是真的。? – 2012-08-16 03:23:15

回答

4

屢試不爽Access 2003中:

SELECT U.user_id, Count(A.act_status) AS Count_Client_id, Sum(C.client_price) AS 
Sum_client_price, Max(A.act_date) AS Max_act_date 
FROM (tbl_user AS U INNER JOIN tbl_sActivity AS A ON U.user_id = A.user_id) INNER JOIN 
tbl_client AS C ON A.client_id = C.client_id 
GROUP BY U.user_id 

更新產生輸出按例如:

SELECT U.user_id, Count(A.client_id) AS CountOfclient_id, Sum(A.client_price) AS 
SumOfclient_price, S.Max_act_date 
FROM (tbl_user AS U INNER JOIN [SELECT DISTINCT tbl_SActivity.client_id, 
tbl_SActivity.user_id, tbl_client.client_price 
FROM tbl_SActivity INNER JOIN tbl_client ON tbl_SActivity.client_id = 
tbl_client.client_id]. AS A ON U.user_id = A.user_id) INNER JOIN [SELECT MAX(act_date) AS Max_act_date, user_id FROM tbl_sActivity GROUP by user_id]. AS S ON U.user_id = S.user_id 
GROUP BY U.user_id, S.Max_act_date 
+0

這是我能看到的唯一的MS Access SQL答案。 – Fionnuala 2012-08-16 09:45:07

+0

恩..但結果仍然不正確。你會發現Sql應該只計算唯一的client_id,其中user_id(1)應該只返回3個client_id(s),對於Sum(client_price),它應該爲user_id(1)返回2100。只有返回正確輸出的MAX_act_date。 @Mat Donnan – Alxan 2012-08-16 12:34:30

+0

@Alxan請看看我更新的SQL,它現在匹配問題 – 2012-08-16 12:50:55

-2

計數(不同的client_id)嘗試此查詢。我測試過:

SELECT a.user_id , 
    COUNT(DISTINCT b.client_ID) Count_Client_id, 
    SUM(DISTINCT c.client_Price) Sum_client_price, 
    MAX(b.act_date) Max_act_date 
FROM tbl_user a 
     INNER JOIN tbl_sActivity b 
      on a.user_id = b.user_id 
     INNER JOIN tbl_client c 
      on b.client_ID = c.client_ID 
GROUP BY a.user_ID 

享受。

+0

對不起..這也沒有工作的MS訪問。 – Alxan 2012-08-16 03:59:19

+0

我的查詢是正確的。我在mssql中測試過它。 – 2012-08-16 10:06:32

+1

標籤說MS Access。 SQL是一個通用標記,並不意味着MS SQL。您的查詢不會在OP正在使用的數據庫中運行。 – Fionnuala 2012-08-16 11:06:29