子查询“查找购买次数最多的客户”

时间:2013-10-05 22:29:07

标签: 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.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

如果算术函数应该放在select外部查询或子查询中,我很困惑,因为我应该放置子查询(在select行,from,having或where子句中)。我知道有嵌套子查询,相关子查询,多列子查询,多行子查询,单行子查询。顺便说一句,我试图在Oracle中这样做。 这是我的结果的图像,除了我从数量列中删除了总和。另外,更新了链接。

http://i1294.photobucket.com/albums/b618/uRsh3RRaYm0nD/Capture100_zps1f951b07.jpg

对不起,我忘了包含第四个表,因为您可以看到产品表和产品类型表中有两个名称列。不同之处在于产品表中“名称”是产品的特定名称,如我的链接所示。产品类型“名称”列是产品的更通用的名称,例如books,dvds,cds等。我需要在产品类型中包含“我的查询中的名称列而不是产品的名称列。”因此,最终结果应该是看起来像这样

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

4 个答案:

答案 0 :(得分:1)

这是一种方法。它使用分析功能按总购买量订购客户:row_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

<强> Example Fiddle

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

Select
  c.FullName,
  c.name,
  c.price,
  c.quantity
From (
  Select
    c.first_name ||' '|| c.last_name FullName,
    pt.name,
    sum(p.price * ps.quantity) price,
    sum(ps.quantity) quantity,
    row_number() over (partition by pt.name order by Sum(Quantity) desc) r
  From
    Purchases ps
      Inner Join
    Customers c
      On ps.Customer_ID = c.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
  Group By
    c.first_name ||' '|| c.last_name,
    pt.name
  ) c
Where
  c.r = 1

<强> Example Fiddle

答案 1 :(得分:0)

这是一般性的想法。您可以根据数据库表进行调整。

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

答案 2 :(得分:0)

SELECT CLIENT.CLIENTNO, CLIENT.CNAME, SUM(PURCHASE.AMOUNT) AS AMOUNT
FROM CLIENT
INNER JOIN PURCHASE
ON CLIENT.CLIENTNO = PURCHASE.CLIENTNO
WHERE CLIENT.CLIENTNO IN (
    SELECT CLIENTNO
    FROM (
        SELECT PURCHASE.CLIENTNO, SUM(PURCHASE.AMOUNT) AS AMOUNT
        FROM PURCHASE
        GROUP BY PURCHASE.CLIENTNO
        ORDER BY AMOUNT DESC
    )
WHERE ROWNUM = 1)
GROUP BY CLIENT.CLIENTNO, CLIENT.CNAME;

答案 3 :(得分:0)

select first_name ||' '|| last_name "FullName",name,quantity from customers,purchases,products where products.product_id = purchases.product_id and purchases.customer_id = customers.customer_id order by quantity;

这是您想要的查询