我在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;
你能後你試一下查詢?將幫助我們知道從哪裏開始幫助您找到正確的解決方案。 – 2012-08-16 02:57:33
+1,我abt寫這個查詢,但想看看做了什麼。可以做作業嗎? ;) – Abhinav 2012-08-16 03:02:57
你確定你的結果是真的。? – 2012-08-16 03:23:15