sql - 左连接并对每一行求和
问题描述
我有一个名为 orders(orderID、customerID、purchaseDate 和 paymentType)和 orderLine(orderID、upc、productName、quantity、price)的表。我正在尝试加入这两个表,以便它主要显示订单表,然后在最右侧为每个订单提供一个总价格列。这是我到目前为止所拥有的,但它将每一行的总数加到一个中。我希望每个单独的行都有自己的总和,方法是根据 orderID 乘以数量*价格。
SELECT orders.orderID, SUM(orderLine.price * orderLine.quantity)
FROM orderLine
LEFT JOIN orders
ON orders.orderID = orderLine.orderID
解决方案
您需要添加 group by 子句
SELECT orders.orderID, SUM(orderLine.price * orderLine.quantity)
FROM orderLine
LEFT JOIN orders
ON orders.orderID = orderLine.orderID
grou by orders.orderID
推荐阅读
- javascript - bootstrap 4手风琴在角度项目中动态加载
- google-cloud-platform - 使用 DataflowTempleOperator 的 Composer Dataflow 作业错误
- r - 从 Rstudio 连接到 SQL 数据库时出错
- pandas - Pandas for循环将每日数据提取到csv
- sql - 生成带有行号postgres sql的表
- gramex - 面对“Uncaught SyntaxError: Unexpected token '{'”
- data-binding - 发送消息后 NativeScript 空文本字段
- firebase-mlkit - 如何在 react native 中从 ml-vision 迁移到 Mlkit?
- java - org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“userServiceImpl”的bean时出错
- gitlab - 如何在 .gitlab-ci.yml 中获取 MR 编号?