【发布时间】:2023-03-17 17:00:02
【问题描述】:
我正在使用 sql server,我有一个名为 Table 的表,看起来像这样。
| SenderId | ReciverId | ItemCount |
|---|---|---|
| 1 | 2 | 5 |
| 1 | 4 | 3 |
| 1 | 6 | 4 |
| 2 | 1 | 2 |
| 2 | 5 | 6 |
| 3 | 1 | 1 |
| 4 | 3 | 7 |
| 4 | 5 | 4 |
| 5 | 2 | 6 |
| 5 | 4 | 2 |
| 5 | 6 | 6 |
| 6 | 4 | 3 |
我想将 SenderIds 和 ReciverIds 分组。然后我想合计这些组之间的 ItemCount。分组如下。
SenderId 1 = 第一个。 SenderIds 2,3 = 第二。 SenderIds = 4,5,6 = 第三 ReciverId 1 = 第一个。 ReciverIds 2,3 = 第二。 ReciverIds = 4,5,6 = 第三
我想返回以下内容
| SenderGroup | ReceiverGroup | ItemCount |
|---|---|---|
| First | First | 0 |
| First | Second | 5 |
| First | Third | 7 |
| Second | First | 3 |
| Second | Second | 0 |
| Second | Third | 6 |
| Third | First | 0 |
| Third | Second | 13 |
| Third | Third | 15 |
我尝试了几个不同的查询,但都没有成功。这是我目前所拥有的一些内容。
SELECT 'First' AS SenderGroup, COUNT(ItemCount) AS ItemCount
FROM Table
WHERE SenderId IN (1)
【问题讨论】:
标签: sql sql-server