我得到了基础数据结构:
+-----+-------+-----------+----------+---------+----------------+-------+---------+------+------+------+----------------+-----------------+--------------+
| Row | Order | Extension | Position | Amount1 | Running number | Type | Amount2 | Dim1 | Dim2 | Dim3 | Text | Additional Info | Articlegroup |
+-----+-------+-----------+----------+---------+----------------+-------+---------+------+------+------+----------------+-----------------+--------------+
| 1 | 1 | A | 26 | 1 | 10 | Wood | 1 | 1163 | 32 | 17 | Very good wood | Wood-1 | Wood Product |
| 2 | 1 | A | 26 | 1 | 12 | Wood | 1 | 1673 | 32 | 17 | Very good wood | Wood-1 | Wood Product |
| 3 | 1 | A | 26 | 1 | 14 | Glass | 1 | 1163 | 32 | 17 | Very good wood | Wood-1 | Wood Product |
| 4 | 2 | A | 29 | 1 | 16 | Wood | 1 | 1673 | 32 | 17 | Very good wood | Wood-1 | Wood Product |
| 5 | 2 | A | 29 | 1 | 48 | Wood | 1 | 1356 | 32 | 17 | Very good wood | Wood-1 | Wood Product |
+-----+-------+-----------+----------+---------+----------------+-------+---------+------+------+------+----------------+-----------------+--------------+
我想要两件事: 1)我想要另一列乘以Amount1 * Amount2 * Dim1 / 1000(由于第2点,这可能有点棘手)
2)我想要以下输出:
+-------+-------+----------------+------------+
| Order | Type | Text | SumProduct |
+-------+-------+----------------+------------+
| 1 | Wood | Very good wood | 2836 |
| 1 | Glass | Very good wood | 1163 |
| 2 | Wood | Very good wood | 3029 |
+-------+-------+----------------+------------+
总之,我希望按行,类型和文本对行进行分组,并在第1点完成计算的总和。
可能使用子查询实现第1点,只需对行进行求和。 Row是唯一标识符,因此我们可能需要在子查询中包含它以创建关系。
第2点是我迷失的东西。文本是一个文本字段,因此我们可能需要找到一种不同的分组方式。当我尝试对行进行分组时,其他列受影响,因为您必须在分组中使用它们 - 我不知道为什么。
答案 0 :(得分:0)
您似乎正在寻找SUM
GROUP BY
。
SELECT T.Order, T.Type, T.Text, SUM(T.Amount1*T.Amount2*T.Dim1) SumProduct
FROM YourTable T
GROUP BY T.Order, T.Type, T.Text
在你的问题中,你提到将总和除以1000,但我没有在预期的结果中看到这一点,所以我把它排除了。
是的,你真的有一个名为Order
的专栏吗?这可能需要转义,因为它是许多数据库中的保留字。
答案 1 :(得分:0)
试试这个:
SELECT
Order
,Type
,Text
,SUM( Amount1 * Amount2 * Dim1 / 1000) SumProduct
FROM tablename
GROUP BY
Order
,Type
,Text;