Sql 子查询";寻找购买量最大的客户”;

Sql 子查询";寻找购买量最大的客户”;,sql,oracle,Sql,Oracle,我在创建带有子查询的查询时遇到问题,无法在我的数据库中找到购买量最大的客户。我需要列出他/她的全名、产品名称、价格和数量。这是我到目前为止所拥有的 select first_name ||' '|| last_name "FullName", pt.name p.price, sum(ps.quantity) from customers c join purchases ps on c.customer_id = ps.customer_id join products p on p.

我在创建带有子查询的查询时遇到问题,无法在我的数据库中找到购买量最大的客户。我需要列出他/她的全名、产品名称、价格和数量。这是我到目前为止所拥有的

select first_name ||' '|| last_name "FullName", pt.name p.price,  sum(ps.quantity) 
from customers c 
join purchases ps on c.customer_id = ps.customer_id 
join products p on p.product_id = ps.product_id
join product_types pt on p.product_type_id = pt.product_type_id;
我需要用这三张桌子

客户表

Customer_ID
First_Name
Last_Name
DOB
Phone
Product_ID
Customer_ID
Quantity
Product_ID
Product_Type_ID
Name
Description
Price
Product_Type_ID
Name
采购表

Customer_ID
First_Name
Last_Name
DOB
Phone
Product_ID
Customer_ID
Quantity
Product_ID
Product_Type_ID
Name
Description
Price
Product_Type_ID
Name
产品表

Customer_ID
First_Name
Last_Name
DOB
Phone
Product_ID
Customer_ID
Quantity
Product_ID
Product_Type_ID
Name
Description
Price
Product_Type_ID
Name
产品类型表

Customer_ID
First_Name
Last_Name
DOB
Phone
Product_ID
Customer_ID
Quantity
Product_ID
Product_Type_ID
Name
Description
Price
Product_Type_ID
Name
如果算术函数应该放在select外部查询或子查询中,我不知道应该将子查询放在哪里(在select行、from、having或where子句中)。我知道有嵌套子查询、相关子查询、多列子查询、多行子查询、单行子查询。顺便说一句,我正试图在甲骨文中做到这一点。 这是一张带有我的结果的图片,只是我从数量列中删除了总和。另外,更新了链接

()

很抱歉,我忘了包括第四个表,因为您可以看到有两个名称列,在products表和product type表中。区别在于,在products表中,“Name”是产品的特定名称,如我的链接所示。产品类型“名称”列是产品的更一般的名称,如书籍、DVD、CD等。我需要在查询中包括产品类型“名称”列,而不是产品名称列。因此,最终结果应该如下所示

 FullName  ProductTypeName  Price        Quantity    
 John Brown    Book         Sumof4books   4
 John Brown    DVD          Sumof2DVDs    2
 John Brown    Magazine     Sumof1Mag     1

这里是总体思路。您可以根据数据库表对其进行调整

select fred, barney, maxwilma
from bedrock join
(select max(wilma) maxwilma
from bedrock
group by fred ) flinstone on wilma = maxwilma

这里有一种方法。它使用一个分析函数来按购买总量向客户订购:
row\u number()over(order by sum(quantity)desc)
。如果有多个人拥有相同的数量,这将只挑选一个人

然后,它获取这个客户id并以明显的方式连接其余的表,以获得按产品类型划分的明细

Select
  c.FullName,
  pt.name,
  Sum(p.price * ps.quantity) price,
  sum(ps.quantity) quantity
From (
  Select
    c.Customer_ID,
    c.first_name ||' '|| c.last_name FullName,
    row_number() over (order by Sum(Quantity) desc) r
  From
    Purchases ps
      Inner Join
    Customers c
      On ps.Customer_ID = c.Customer_ID
  Group By
    c.Customer_ID,
    c.first_name ||' '|| c.last_name
  ) c
    Inner Join
  Purchases ps
    On c.Customer_ID = ps.Customer_ID
    Inner Join
  Products p
    On ps.Product_ID = p.Product_ID
    Inner Join
  Product_Types pt
    On p.Product_Type_ID = pt.Product_Type_ID
Where
  c.r = 1
Group By
  c.FullName,
  pt.name

对于第二个问题(显示每种产品类型中谁的数量最多,以及他们在该产品类型上的花费)


这是您想要的查询

您需要更具体一些。添加一些测试数据和期望的答案。您是否在为每个产品寻找一个购买该产品最多的客户?在所有购买中数量最多的单个客户,无论产品是什么?还有什么?我收到了“第27行错误:列:3表或视图不存在”消息。请看第27行,注意我拼错了表名,请修复它。是的,非常感谢。是否可以包括其他产品类型(DVD、视频等)数量最多的客户根据目前的图书结果?我想补偿你的时间,你的PayPal电子邮件是什么?谢谢。