我有一张包含几个值的表格'和相应的'数量',即Value1 = 56 value1qty = 7,value2 = 15 value2qty = 563。共有6个值和数量。
我需要一些如何列出来自所有6个字段的每个唯一值并总计这些值的联合数量。
我不知道从哪里开始。我可以通过一个简单的选择*获得价值和数量,但之后我就迷失了。
感谢任何帮助,甚至只是指着我正确的方向。
编辑以供澄清。
表格如下:
Private Sub test()
Dim R As Long
Dim i As Integer
i = 1
R = 2
Count = 0
Do While Not IsEmpty(Range("A" & R))
If Cells(R, 1).Value = Cells(R + 1, 1).Value Then
Count = Count + 1
Else
i = 1
Do While i <= Count
Cells(R - Count, 2 + (2 * i)).Value = Cells(R - Count + i, 2 + (2 * i))
Cells(R - Count, 3 + (2 * i)).Value = Cells(R - Count + i, 3 + (2 * i))
i = i + 1
Loop
i = 1
Do While i <= Count
Rows(R - Count + i).Delete
i = i + 1
R = R - 1
Loop
Count = 0
End If
R = R + 1
Loop
End Sub
需要结果如下:
ID || value_1 || value_1_qty || value_2 || value_2_qty
---------------------------------------------------------------------------------
1 || 562 || 67 || 637 || 12
2 || 637 || 23 || 768 || 68
答案 0 :(得分:1)
使用UNION
将所有值和数量合并为一列,然后您可以使用SUM()
和GROUP BY
:
SELECT value, SUM(qty)
FROM (
SELECT value_1 AS value, value_1_qty AS qty
FROM yourTable
UNION ALL
SELECT value_2 AS value, value_2_qty AS qty
FROM yourTable
UNION ALL
SELECT value_3 AS value, value_3_qty AS qty
FROM yourTable
UNION ALL
...
) AS x
GROUP BY value
重新设计表可能会更好,因此您没有所有这些不同的列。