Sum SQL PLUS 函数
Sum SQL PLUS function
我正在尝试添加多行以将它们组合成较小的行,这些行按商品编号和订购数量排列
假设我有这样的东西
指挥官
|noArticle|quantity
|10 |100
|10 |0
|10 |0
|20 |20
|20 |20
|20 |20
文章
|noArticle|Description
|10 |potato
|20 |banana
|30 |ham
预期结果:
|noArticle|totalquantity
|10 |100
|20 |60
我的结果:
|noArticle|totalquantity
|10 |160
|20 |160
最后 1 想要 2 行,其中 1 是商品编号,2 是总数量,所以应该是 10 ; 100 和 20 ;60
现在我有这样的东西
SELECT Article.noArticle, SUM (quantity) AS TOTALQUANTITY
FROM LigneCommande, Article
GROUP BY Article.noArticle
ORDER BY Article.noArticle
问题是它对所有数量求和并以 20;160 和 10;160 结尾
我可能遗漏了一个 OVER 函数或无法解决的问题
编辑:好吧让我把事情弄清楚我有 2 tables 一个在这个 table 中有文章编号和数量的叫做 LigneCommande 只有数量大于 0 的文章才会出现.我有另一个名为 Article 的 table,即使没有数量,它也包含所有商品编号。最后,我想要 Article 中的所有商品编号和 Ligne 命令中的所有数量,将这两个组合在一起的键是 noArticle(英语不是我的母语,如果我不清楚,抱歉)
我认为您 sql 中的错误是您还在查询中包含了 LigneCommande table,这似乎没有用。尝试:
SELECT Article.noArticle, SUM (quantity) AS TOTALQUANTITY
FROM Article
GROUP BY Article.noArticle
ORDER BY Article.noArticle
以上假定数量是文章table中的一栏。如果不是这种情况(并且 LigneCommande 有数量),那么您需要将其相应地加入到您的架构中(例如,可能类似于以下内容)
SELECT a.noArticle, SUM (l.quantity) AS TOTALQUANTITY
FROM Article a
Join LigneCommande l on l.noArticle = a.noArticle
GROUP BY a.noArticle
ORDER BY a.noArticle
您可以尝试在分组中使用 rollup modifier 子句:
SELECT Article.noArticle, SUM (quantity) AS TOTALQUANTITY
FROM Article
LEFT JOIN LigneCommande ON Article.noArticle=LigneCommande.noArticle
GROUP BY ROLLUP( Article.noArticle)
您可以在 having 子句中过滤掉总计。
我正在尝试添加多行以将它们组合成较小的行,这些行按商品编号和订购数量排列
假设我有这样的东西
指挥官
|noArticle|quantity
|10 |100
|10 |0
|10 |0
|20 |20
|20 |20
|20 |20
文章
|noArticle|Description
|10 |potato
|20 |banana
|30 |ham
预期结果:
|noArticle|totalquantity
|10 |100
|20 |60
我的结果:
|noArticle|totalquantity
|10 |160
|20 |160
最后 1 想要 2 行,其中 1 是商品编号,2 是总数量,所以应该是 10 ; 100 和 20 ;60
现在我有这样的东西
SELECT Article.noArticle, SUM (quantity) AS TOTALQUANTITY
FROM LigneCommande, Article
GROUP BY Article.noArticle
ORDER BY Article.noArticle
问题是它对所有数量求和并以 20;160 和 10;160 结尾
我可能遗漏了一个 OVER 函数或无法解决的问题
编辑:好吧让我把事情弄清楚我有 2 tables 一个在这个 table 中有文章编号和数量的叫做 LigneCommande 只有数量大于 0 的文章才会出现.我有另一个名为 Article 的 table,即使没有数量,它也包含所有商品编号。最后,我想要 Article 中的所有商品编号和 Ligne 命令中的所有数量,将这两个组合在一起的键是 noArticle(英语不是我的母语,如果我不清楚,抱歉)
我认为您 sql 中的错误是您还在查询中包含了 LigneCommande table,这似乎没有用。尝试:
SELECT Article.noArticle, SUM (quantity) AS TOTALQUANTITY
FROM Article
GROUP BY Article.noArticle
ORDER BY Article.noArticle
以上假定数量是文章table中的一栏。如果不是这种情况(并且 LigneCommande 有数量),那么您需要将其相应地加入到您的架构中(例如,可能类似于以下内容)
SELECT a.noArticle, SUM (l.quantity) AS TOTALQUANTITY
FROM Article a
Join LigneCommande l on l.noArticle = a.noArticle
GROUP BY a.noArticle
ORDER BY a.noArticle
您可以尝试在分组中使用 rollup modifier 子句:
SELECT Article.noArticle, SUM (quantity) AS TOTALQUANTITY
FROM Article
LEFT JOIN LigneCommande ON Article.noArticle=LigneCommande.noArticle
GROUP BY ROLLUP( Article.noArticle)
您可以在 having 子句中过滤掉总计。