我有一個查詢,顯示通過我的系統去年發送了多少個消息,按月分組。完美的作品!按2列分組
結果看起來像這樣:
+------+-------+--------+--------+--------+
| Year | Month | Type 1 | Type 2 | Type 3 |
+------+-------+--------+--------+--------+
| 2013 | 10 | 0 | 2 | 3 |
| 2013 | 11 | 4 | 21 | 56 |
| 2013 | 12 | 1 | 10 | 16 |
| 2014 | 1 | 2 | 10 | 52 |
| 2014 | 2 | 1 | 62 | 118 |
+------+-------+--------+--------+--------+
(類型1,2和3是簡單地不同類型的用戶-ignore此)
然而,我想避免所述相同的接收器(msg_receiver)可以在結果集中顯示兩次,每個月。
因此,如果用戶44和39在12月向用戶70發送消息,則user_id 70將僅在12月被計數一次。目前,他將出現兩次。
下面是我的查詢:
SELECT
Year(m.msg_date) as year,
Month(m.msg_date) as month,
sum(u.type = '1') as type_1,
Sum(u.type = '2') as type_2,
sum(u.type = '7') as type_3
FROM
messages m
INNER JOIN
users u ON u.user_id = m.msg_sender
WHERE
m.msg_date >= CURDATE() - INTERVAL 1 YEAR
AND month(msg_date) != month(curdate())
GROUP BY
Month(m.msg_date) -- , m.msg_receiver (this does not work, it will no longer group by each month/year).
ORDER BY
msg_date
邏輯答案,就在我的選擇是,以第一組由一個月,然後USER_ID(或副通過)。但如果我這樣做,結果看起來很奇怪。請參閱:
使用GROUP BY Month(m.msg_date), u.user_id
+------+-------+--------+--------+--------+
| Year | Month | Type 1 | Type 2 | Type 3 |
+------+-------+--------+--------+--------+
| 2013 | 10 | 0 | 1 | 0 |
| 2013 | 10 | 0 | 0 | 1 |
| 2013 | 10 | 0 | 0 | 1 |
| 2013 | 10 | 0 | 1 | 0 |
| 2013 | 10 | 0 | 0 | 1 |
| 2013 | 11 | 0 | 0 | 19 |
| 2013 | 11 | 0 | 1 | 0 |
| 2013 | 11 | 0 | 1 | 0 |
| 2013 | 11 | 0 | 1 | 0 |
| 2013 | 11 | 0 | 1 | 0 |
| 2013 | 11 | 2 | 0 | 0 |
| 2013 | 11 | 0 | 0 | 11 |
+------+-------+--------+--------+--------+
它沒有GROUP BY個月了,因爲它應該。
任何想法?
編輯
只是爲了澄清我想要什麼來實現的,因爲人們已經有點糊塗了。想象一下這種情況:
It is December 2013.
USER 1 has written 5 messages to USER 2 (this should count as 1 in december)
USER 4 has written 1 message to USER 4 (this should count as 1 in december)
USER 3 has written 2 messages to USER 4 and 2 (this should count as 2 in december).
The totals of the month would then be 4. Because there has been 4 conversations.
它有道理嗎?我發現我的自我經常在如何正確表達我的自我和理解方面掙扎。
考慮提供適當的DDL(和/或sqlfiddle)連同所需的結果集 – Strawberry
爲了回答這個問題 - 一個問題 - 如果接收方發送多條消息,並且每條消息都是不同的類型,那麼您要計算接收方的類型? – user158017
我明白了,對於混亂感到抱歉。我編輯了我的答案來解釋 – FooBar