[英]SELECT a column and SUM() of values from another table in SQL
I'm pretty new with SQL, and this is giving me trouble. 我对SQL很陌生,这给我带来了麻烦。 The idea is that I have several tables. 这个想法是我有几个表。 Here are the relevant tables and columns: 以下是相关的表和列:
customers:
customer_id, customer_name
orders:
order_id, customer_id
orderline:
order_id, item_id, order_qty
items:
item_id, unit_price
I need to return customer_name as well as total revenue from that customer (calculated as item_price * order_qty * 2). 我需要返回customer_name以及该客户的总收入(按item_price * order_qty * 2计算)。
Here's what I have written: 这是我写的:
SELECT customers.customer_name, sum(revenue)
FROM SELECT orderline.order_qty * items.unit_value * 2 AS revenue
FROM orderline
INNER JOIN orders
ON orderline.order_id = orders.order_id
INNER JOIN customers
ON revenue.customer_id = customers.customer_id;
This throws a syntax error and I'm not really sure how to proceed. 这将引发语法错误,我不确定如何继续。
This is only one example of this type of problem that I need to work out, so more generalized answers would be helpful. 这只是我需要解决的这类问题的一个示例,因此更笼统的答案会有所帮助。
Thanks in advance! 提前致谢!
EDIT: 编辑:
With help from answers I ended up with this code, which just gets total revenue and puts it next to the first person in the DB's name. 在答案的帮助下,我最终得到了这段代码,该代码仅获得总收入,并将其置于数据库名称的第一位。 What did I get wrong here? 我在这里错了什么?
SELECT customers.customer_name, sum(revenue)
FROM(SELECT orderline.order_qty * items.unit_price * 2 AS revenue, orders.customer_id AS CustomerID
FROM( orderline
INNER JOIN orders
ON orderline.order_id = orders.order_id
INNER JOIN items
ON orderline.item_id = items.item_id)) CustomerOrders
INNER JOIN customers
ON CustomerOrders.CustomerID = customers.customer_id;
SELECT c.customer_name,
sum(COALESCE(ol.order_qty,0) * COALESCE(i.unit_value,0) * 2)
FROM customers c
INNER JOIN orders o
ON o.customer_id = c.customer_id;
INNER JOIN orderline ol
ON ol.order_id = o.order_id
INNER JOIN items i
ON i.item_id = ol.item_id
GROUP BY c.customer_id
A couple issues with your query. 您的查询有几个问题。 First, you need to scope your subquery and alias it: 首先,您需要确定子查询的范围并为其命名别名:
(SELECT orderline.order_qty * items.unit_value * 2 AS revenue
FROM orderline
INNER JOIN orders
ON orderline.order_id = orders.order_id) CustomerOrders
Secondly, you need to select more than the revenue in the subquery since you are joining it to your customers table 其次,您需要选择比子查询中更多的收入,因为您正在将其加入到customers表中
(SELECT
orderline.order_qty * items.unit_value * 2 AS revenue,
orders.customer_id AS CustomerId
FROM
orderline
INNER JOIN orders ON orderline.order_id = orders.order_id) CustomerOrders
Then you need to use the subquery alias in the join to the customers table and wrap it all up in a group by customer_id and CustomerOrders.Revenue 然后,您需要使用joins到customers表中的子查询别名,并将其全部按customer_id和CustomerOrders.Group收入一个组中。
I would tend to do it differently. 我倾向于做不同的事情。 I'd start with selecting from the customer table, because that is the base of what you are looking for. 我将从客户表中进行选择,因为这是您要寻找的基础。 Then I'd do a cross apply on the orders that would all aggregating the order revenue in the subquery. 然后,我将对所有交叉汇总子查询中的订单收入的订单进行交叉应用。 It would look like this (tsql, you could do the same in mysql with a join with some aggregation): 它看起来像这样(tsql,您可以在mysql中通过一些聚合进行相同的操作):
SELECT
customers.customer_name,
ISNULL(customerOrders.Revenue, 0) AS Revenue
FROM
customers
OUTER APPLY (
SELECT
SUM (orderline.order_qty * items.unit_value * 2) AS Revenue
FROM
orders
INNER JOIN
orderline ON orders.order_id = orderline.order_id
INNER JOIN
items on orderline.item_id = items.item_id
WHERE
orders.customer_id = customers.customer_id
) CustomerOrders
In this case, the subquery aggregates all your orders for you and only returns one row per customer, so no extraneous returned data. 在这种情况下,子查询将为您汇总所有订单,并且每个客户仅返回一行,因此不会有多余的返回数据。 Since it's an outer apply, it will also return null for customers with no orders. 由于是外部应用,因此对于没有订单的客户,它也将返回null。 You could change it to a CROSS APPLY and it will filter out customers with no orders (like an INNER JOIN). 您可以将其更改为CROSS APPLY,它将过滤掉没有订单的客户(例如INNER JOIN)。
select customer_name, sum(item_price * order_qty * 2) as total_revenue
from (
select * from customers
inner join orders using(customer_id)
inner join orderline using(order_id)
inner join items using(item_id)
)
group by customer_name
select
c.customer_name,
r.revenue
from
customers c
inner join
orders ord on
ord.customer_id = c.customer_id
inner join
(select i.item_id, o.order_id, sum(o.order_qty * items.unit_value * 2) as revenue
from orderline o
inner join items i on
i.item_id = o.item_id
group by o.order_id, i.item_id) as r on r.order_id = o.order_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.