mysql - SQL Sum 没有返回正确的数据

标签 mysql sql-server asp-classic openedge progress-db

我的 Progress 数据库的示例,opdetail 表

invoice  invline    article   size    qty
----------------------------------------
905155   1          Shoe      10      5
905155   2          Slipper   3       2
905155   2          Slipper   4       6
905155   2          Slipper   5       1
905156   1          Boot      10      1 
905156   1          Boot      11      1
905157   1          Slipper   5       4       
905157   2          Shoe      8       6

a simple SQL select statement, run from the OpenEdge editor returns just what I need, a list of invoices with their total quantities:-

SELECT invoice, sum(qty) FROM opdetail GROUP BY qty ORDER BY invoice ASC
905155  14
905156   2
905157  10

HOWEVER:- When run from an ASP page via DSN I have to list both fields in the GROUP BY otherwise progress returns a GROUP BY error

SELECT invoice, sum(qty) FROM opdetail GROUP BY qty, invoice ORDER BY invoice ASC
905155   5
905155   9
905156   2 
905157   4
905157   6

它没有总结数量,而且似乎考虑了行号,即使行号在我的 sql 语句中没有任何作用。任何人都可以对此有所了解,或者我如何在考虑行号的情况下对总数量求和?谢谢!

最佳答案

您在聚合函数中使用 qty 然后在 group by 上使用这没有任何意义,您应该在一些上使用 group by其他栏目作为

SELECT 
invoice, 
sum(qty) FROM opdetail 
GROUP BY invoice ORDER BY invoice ASC

关于mysql - SQL Sum 没有返回正确的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30097769/

相关文章:

php - mysql按1+排序,最后追加0

sql - 根据另一个数据库的查询结果查询一个数据库

服务器 2008 中一个文件夹的 IIS 安全登录弹出窗口

object - 我什么时候需要在经典 asp 中将对象设置为空?

jquery - FullCalender.io 与经典 ASP

php - 面向 PHP 开发人员的 PostgreSQL 和 MySQL 之间的差异

mysql - 我想显示最高的号码。剧集数

sql-server - 如何查明 SQL Server 上安装了哪个 Service Pack?

mysql - Visual Basic : DataTable. 搜索过滤器参数

sql - 使用目标为列的 MySQL 的 "IN"函数?