城市客户的总销售额

时间:2014-12-30 18:46:42

标签: sql sql-server

我有以下SQL语句,显示已完成至少2个订单的城市客户的总销售额。但是,我只想说明有人至少完成了2个订单的城市/城市,并且有两个客户住在这个城市/城市,所以我想要做的就是挑选城市/城市客户至少订购了2个订单,但随后显示了生活在这个城市的客户的总销售额,即使其他客户只订购了一个订单,如果有COUNT的比较对帐,怎么办呢? - 操作能够显示城市中所有客户的销售额,如果是 - 如何说明?

SELECT c.CityName, SUM(p.Price * o2.Orderquantity) AS 'TotalSalesAmount'
FROM Customers c, Order1 o1, Orderrader o2, Products p,
(SELECT o1.CustomerNr 
FROM Order1 o1
GROUP BY o1.CustomerNr 
HAVING COUNT(o1.CustomerNr) >= 2)
AS a WHERE c.CustomerNr = a.CustomerNr AND c.CustomerNr = o1.CustomerNr
AND o1.Ordernr = o2.Ordernr AND o2.Productnr = p.Productnr
GROUP BY c.CityName

我使用的表的结构如下:

'Customers' has the columns: CustomerNr, City Name
'Order1' has columns: Ordernr, CustomerNr
'Order2' has columns: Ordernr, Productnr, Order quantity
'Products' has columns: ProductNr, Price

示例数据:表格'客户'

的示例数据
- CustomerNr  CityName:
- 01109       New York
- 01999       Los Angeles
- 20090       New York

' Order1'的样本数据:

- Ordernr  CustomerNr
- 1306     01109
- 1307     01109
- 1308     20090

' Order2'的示例数据:

- OrderNr   ProductNr  Order quantity:
- 1306      15-116     3
- 1306      46-701     2
- 1307      15-96      1
- 1308      17-91      1

(等...)

'产品'

的示例数据
- ProductNr  Price:
- 15-116     44.00
- 15-96      28.50
- 46-701     3000.00
- 17-91      200.00

等...

根据上面的SQL语句和示例数据,我想要的结果是:

- CityName  TotalSalesAmount:
- New York   6360.50

4 个答案:

答案 0 :(得分:1)

尝试使用子查询。我知道这看起来有点讨厌但必须正常工作。

原来您在Order1示例中对同一个CustomerNr有重复的Ordernr(1306)。我假设您的实际数据可能不是这种情况。

SELECT c.CityName, 
(Select SUM(order2.quantity * products.Price) from order1 
INNER JOIN Customers On Customers.CustomerNr=order1.CustomerNr 
INNER JOIN Order2 ON Order2.Ordernr=Order1.Ordernr 
INNER JOIN Products ON Products.ProductNr=Order2.ProductNr 
WHERE Customers.CityName=c.CityName) AS 'TotalSalesAmount'
FROM Order1 o1
INNER JOIN (SELECT o1.CustomerNr 
        FROM Order1 o1 
        GROUP BY o1.CustomerNr 
        HAVING COUNT(o1.CustomerNr) >= 2
       ) AS a ON o1.CustomerNr = a.CustomerNr 
INNER JOIN Order2 o2 ON o1.Ordernr = o2.Ordernr 
INNER JOIN Customers c ON o1.CustomerNr = c.CustomerNr
INNER JOIN Products P ON o2.ProductNr = P.ProductNr
GROUP BY c.CityName;

答案 1 :(得分:0)

给这一点。

SELECT CityName, SUM(quantity * Price) AS TotalSalesAmount
FROM (
    SELECT c.CityName, p.ProductNr, b.quantity, p.Price, (b.quantity * p.Price) as total --, SUM(b.quantity * p.Price) AS TotalSalesAmount
    FROM customers c
        INNER JOIN order1 a ON c.CustomerNr = a.CustomerNr
        INNER JOIN order2 b ON a.Ordernr = b.Ordernr
        INNER JOIN products p ON b.Productnr = p.ProductNr
    GROUP BY c.CityName, p.ProductNr, b.quantity, p.Price
    ) AS cust
GROUP BY CityName
HAVING COUNT(cityname) >= 2 

答案 2 :(得分:0)

试试这个。

SELECT c.CityName,
       Sum(o2.Orderquantity * p.Price) Total_sale
FROM   Customers C
       JOIN (SELECT o.CustomerNr
             FROM   Order1 o
             GROUP  BY o.CustomerNr
             HAVING Count(o.CustomerNr) >= 2) su
         ON c.CustomerNr = su.CustomerNr
       JOIN Customers c1
         ON c1.CityName = c.CityName
       JOIN Order1 o1
         ON o1.CustomerNr = c1.CustomerNr
       JOIN Order2 o2
         ON o2.Ordernr = o1.Ordernr
       JOIN Products P
         ON o2.ProductNr = P.ProductNr
GROUP  BY c.CityName   

答案 3 :(得分:0)

我已将您的问题解释为“我需要按城市划分所有销售额的总和,对于任何一个客户订购了多个订单的城市。”

select 
    c.City,
    SUM(p.Price * o2.Orderquantity) AS 'TotalSalesAmount' 
from
    (select c.City
    from @t_Customers c
    inner join @t_Order1 o1
        on o1.CustomerNr = c.CustomerNr
    group by c.City
    having count(c.City) > 1) as ct
    inner join @t_Customers c
        on c.City = ct.City
    inner join @t_Order1 o1
        on o1.CustomerNr = c.CustomerNr
    inner join @t_Order2 o2
        on o2.OrderNr = o1.Ordernr
    inner join @t_Products p
        on p.ProductNr = o2.ProductNr
group by c.City