简体   繁体   English

SQL查询三个表的计算

[英]SQL Query for three tables with calculation

I was given an interview question and I cannot resolve it. 我收到了面试问题,但无法解决。 I'm not in the SQL field, and it was more to show problem solving ability than SQL ability. 我不在SQL领域,更多的是显示解决问题的能力而不是SQL的能力。 I'd still like to figure it out though! 我还是想弄清楚!

It was in MS SQL server, but a generic SQL answer is acceptable. 它在MS SQL Server中,但是可以接受通用的SQL答案。

I have 3 tables: Sales, Customers, Products and NO NULL values in the tables. 我有3个表:表中的Sales,Customers,Products和NO NULL值。

Customers : Customer_ID, Customer_Name 客户 :Customer_ID,Customer_Name

Products : Product_ID, Product_Price 产品 :Product_ID,Product_Price

Sales : Customer_ID, Product_ID, Number_Purchased 销售 :客户编号,产品编号,已购买的编号

They want me to display, the client who has paid the most, to the client who has paid the least. 他们要我向支付最低的客户展示支付最高的客户。

So I need to link the Customer ID from Customers to the Customer ID in Sales and then the sales to the product price and work out Price * Number Purchased and assign it to the correct person. 因此,我需要将“客户”的“客户ID”链接到“销售”中的“客户ID”,然后将“销售”链接到产品价格,然后计算出“价格*购买数量”并将其分配给正确的人。

I tried something like this at the time: (obviously wrong) 当时我尝试过这样的事情:(显然是错误的)

SELECT Customers.Customer_ID, Customers.Customer_Name, SUM(Sales.Number_Purchased *Products.Product_Price) as Total
FROM (Customers 
INNER JOIN Products 
ON Customers.Customer_ID = Products.Customer_ID)
INNER JOIN Sales ON 
Products.Product_ID = Sales.Product_ID
GROUP BY Customers.Customer_ID, Customers.Customer_Name

Obviously I'm not good with SQL but if somone can give me a shove in the right direction to solving this (second Interview is in a few hours!) I would really appreciate it! 显然,我对SQL不满意,但是如果somone能给我一个正确的方向解决这个问题(第二次面试将在几个小时内完成!),我将不胜感激! I've gotten myself tied in knots. 我陷入困境。

If you want the list sorted from paid the most to paid the least, add: 如果您希望列表从付款最高的到付款最低的排序,请添加:

ORDER BY SUM(Sales.Number_Purchased * Products.Product_Price) DESC

Additionally, you have to change some of the JOIN conditions, since you are not establishing the correct connection between the tables. 此外,由于没有在表之间建立正确的连接,因此您必须更改某些JOIN条件。

And since you are doing this in MS-ACCESS, you have to wrap the JOINS with parenthesis 并且由于您是在MS-ACCESS中执行此操作的,因此必须用括号将JOINS包装起来

It should be: 它应该是:

SELECT Customers.Customer_ID, 
       Customers.Customer_Name,
       SUM(Sales.Number_Purchased * Products.Product_Price) AS Total
FROM ((Customers 
INNER JOIN Sales ON Sales.Customer_ID = Customers.Customer_ID)
INNER JOIN Products ON Sales.Product_ID = Products.Product_ID)
GROUP BY Customers.Customer_ID, Customers.Customer_Name
ORDER BY SUM(Sales.Number_Purchased * Products.Product_Price) DESC

I'd prefer to approach this question in the following steps. 我希望在以下步骤中解决这个问题。

First of all, find out the total of each purchase done by all the customers. 首先,找出所有客户进行的每次购买的总金额。

SELECT Customer_ID, SUM (Sales.Number_Purchased * Products.Product_Price)
FROM Sales
INNER JOIN Products ON Products.Product_ID = Sales.Product_ID
GROUP BY Customer_ID

Then we would like to know the customer name. 然后,我们想知道客户名称。 So we will inner join the Customes table. 因此,我们将内部联接Customes表。

SELECT Customer_ID, Customer_Name, SUM (Sales.Number_Purchased * Products.Product_Price)
FROM Sales
INNER JOIN Products ON Products.Product_ID = Sales.Product_ID
INNER JOIN Customers ON Customers.Customer_ID = Sales.Customer_ID
GROUP BY Customer_ID

Finally, you can do a ORDER BY statement to find out the ones who paid the most and the least. 最后,您可以执行ORDER BY语句来找出支付最高和最低的人。

Take note that in your code, Products.Customer_ID is not allowed because Customer_ID is not one of the columns in the Products table. 需注意,在你的代码,Products.Customer_ID是不允许的,因为CUSTOMER_ID不在Products表中的一列。

EDIT Oops, there is an ambiguous column in my second SQL. 编辑糟糕,我的第二个SQL中有一个歧义的列。 It should be "SELECT Customers.Customer_ID" because the column name Customer_ID is used in two different tables. 它应该是“ SELECT Customers.Customer_ID”,因为在两个不同的表中使用了列名Customer_ID。

Boy have I been in your shoes, half the time not allowed to normalize tables in a DB :( 男孩,我一直在你的鞋子里,一半的时间不允许规范数据库中的表:(

Any who i've quickly looked so forgive the syntax errors. 我很快看过的任何人都可以原谅语法错误。 but you will get the idea: 但您会明白:

SELECT Customers.Customer_ID, 
    Customers.Customer_Name, 
    SUM(Sales.Number_Purchased *Products.Product_Price) as Total
FROM Customers
LEFT JOIN SALES
ON customers.customer_ID = Sales.Customer_ID
left JOIN Products
ON  Sales.productid = products.product_ID
Customers.Customer_ID, Customers.Customer_Name
GROUP BY Customers.Customer_ID, Customers.Customer_Name
ORDER BY SUM(Sales.Number_Purchased * Products.Product_Price) DESC

Cant check it but basically you were right but not on joins. 不能检查它,但基本上您是正确的,但不是联接。

Inner join will only get matching results. 内部联接只会得到匹配的结果。 But what about users who haven't brought anything? 但是,什么都没有带来的用户呢? They wont exist in the sales part so they wont exist in your results, which in the job i was in was a bad thing as they want see all customers. 他们不会出现在销售部分中,因此也不会出现在您的结果中,这对我来说是一件坏事,因为他们希望看到所有客户。 This allows you to pull back all of them, and then if you need to, filter out the null sales. 这使您可以撤回所有这些交易,然后根据需要过滤掉零销售。 Usually its best to zero value them, you can then provide information on customers who haven't spent. 通常最好将其零价值,然后您可以提供有关未消费客户的信息。 (added info makes them happy ;) ) (添加的信息使他们感到高兴;))

SO, use a left join, you will get every customer regardless of spend. 因此,使用左联接,无论花费多少,您都将获得每个客户。 match that up then to the products remember you want to keep all sales, and making sure there isnt a problem with missing data, use another left. 然后将其与产品相匹配,请记住您要保留所有销售额,并确保没有数据丢失问题,请使用另一个左键。 then really just use an order by, and done. 然后真的只是使用命令来完成

hope this helps 希望这可以帮助

There are two things you missed. 您错过了两件事。 A) Links between tables are incorrect. A)表之间的链接不正确。 Following the key columns is useful exercise B) As Filipe pointed out the ORDER BY clause does the sorting 紧随键列是有用的练习B)正如Filipe指出的那样,ORDER BY子句进行了排序

SELECT 
  Customers.Customer_ID, Customers.Customer_Name, 
  SUM(Sales.Number_Purchased *Products.Product_Price) as Total
FROM Customers 
  -- connect sales to customers
  INNER JOIN Sales ON 
    ON Sales.Customer_ID = Customers.Customer_ID
  -- connect products to sales
 INNER JOIN Products 
   Products.Product_ID = Sales.Product_ID
-- group to deliver an aggregate
GROUP BY Customers.Customer_ID, Customers.Customer_Name
-- sort
ORDER BY SUM(Sales.Number_Purchased * Products.Product_Price) DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM