从SQL中的另一个表中选择值的列和SUM()

时间:2016-06-13 19:21:28

标签: mysql sql select sum

我对SQL很新,这给我带来了麻烦。我的想法是我有几张桌子。以下是相关的表格和列:

customers:

customer_id, customer_name


orders:

order_id, customer_id


orderline:

order_id, item_id, order_qty


items:

item_id, unit_price

我需要返回customer_name以及该客户的总收入(计算为item_price * order_qty * 2)。

这是我写的:

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;

这会引发语法错误,我不确定如何继续。

这只是我需要解决的这类问题的一个例子,因此更通用的答案会有所帮助。

提前致谢!

编辑:

在答案的帮助下,我最终得到了这个代码,它只获得总收入,并将其放在数据库名称中的第一个人旁边。我在这里弄错了什么?

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;

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您的查询有两个问题。 首先,您需要确定子查询的范围并将其别名:

(SELECT orderline.order_qty * items.unit_value * 2 AS revenue
    FROM orderline
        INNER JOIN orders
        ON orderline.order_id = orders.order_id) CustomerOrders

其次,您需要选择超过子查询中的收入,因为您要将其加入到客户表中

(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

然后,您需要在customers表的连接中使用子查询别名,并通过customer_id和CustomerOrders.Revenue将其全部包装在一个组中

我倾向于采用不同的方式。我首先从客户表中进行选择,因为这是您正在寻找的基础。然后我会对订单进行交叉申请,这些订单将汇总子查询中的订单收入。它看起来像这样(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

在这种情况下,子查询会为您汇总所有订单,并且每个客户只返回一行,因此没有多余的返回数据。由于它是外部应用,因此对于没有订单的客户,它也将返回null。您可以将其更改为CROSS APPLY,它将过滤掉没有订单的客户(如INNER JOIN)。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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