我在計算以下mysql查詢時遇到了一些麻煩。mysql-group by multiple columns and sum
這意味着一種時間跟蹤,用戶根據他們所做的任務具有一定的費率。我需要有一個總結,所以我知道在本週結束時要向用戶付款。
-----------tSessions---------------------------
|id |userid |typeid |session_length_min
-----------------------------------------------
|1 |1 |1 |30
-----------------------------------------------
|2 |1 |1 |45
-----------------------------------------------
|3 |1 |2 |(null)
-----------------------------------------------
|4 |2 |2 |(null)
-----------------------------------------------
-----------tUsers-----------------------------------------------------------------------------------
|id |name |rate_cleaning_30_min |rate_cleaning_45_min |rate_kitchenwork |rate_dogwalking
----------------------------------------------------------------------------------------------------
|1 |Tom |30 |50 |40 |20
----------------------------------------------------------------------------------------------------
|2 |Joe |35 |60 |45 |20
----------------------------------------------------------------------------------------------------
|3 |Dave |40 |60 |30 |10
----------------------------------------------------------------------------------------------------
-----------tTypes----------------
|id |name
---------------------------------
|1 |Cleaning
---------------------------------
|2 |Kitchenwork
---------------------------------
|3 |Dogwalking
---------------------------------
============== Required Result ====================
|username |sessioncount |amount_to_pay
---------------------------------------------------
|Tom |2 |120
---------------------------------------------------
|Joe |1 |45
---------------------------------------------------
下面的查詢是一個,如果我不那麼成功的方法:
SELECT
tSessions.id,
tSessions.userid,
tSessions.typeid,
tSessions.session_length_min,
SUM(tUsers.rate_cleaning_30_min) AS rate_cleaning_30_min_sum,
SUM(tUsers.rate_cleaning_45_min) AS rate_cleaning_45_min_sum,
SUM(tUsers.rate_kitchenwork) AS rate_kitchenwork,
SUM(tUsers.rate_dogwalking) AS rate_dogwalking,
Count(*) AS sessioncount,
FROM
tSessions
INNER JOIN tUsers ON tSessions.userid = tUsers.id
WHERE WEEKOFYEAR(FROM_UNIXTIME(datetime))=WEEKOFYEAR(NOW())-1
GROUP BY
tSessions.userid,
tSessions.typeid
我試過類似的東西,但沒有取得太大的成功: – Phil
問題到底在哪裏?預期產出是多少? – fancyPants
我不知道如何去「需要的結果」 – Phil