【问题标题】:SQL to find the top selling product in AprilSQL 查找 4 月份最畅销的产品
【发布时间】:2019-08-11 22:45:50
【问题描述】:

我需要从我的数据库中找到 4 月份最畅销的产品。 我有两张表,分别称为 sales 和 salesline。

sales 表包括全年的每次销售,salesline 表包括每次销售中售出的每种产品。

销售表包括以下内容:

Sale_ID
Payment_ID
Ship_ID
Sales_Total
Date
Time
Cusotomer_ID

销售线表包括:

Salesline_ID
Product_ID
SalePrice
Sale_ID
Payment ID

谢谢

【问题讨论】:

  • count 行数,group by Product_ID。如果您只想要 4 月,则必须加入 sales 表并在计数之前按日期过滤。
  • 最畅销产品基于salesline 表中的saleprice 列?

标签: mysql sql


【解决方案1】:

如果您想根据SalesLine 表的SalePrice 列找出最畅销的产品,您可以进行以下查询:-

    select sl.Product_ID,sum(sl.SalePrice) Sales
    from
    Sales s
    inner join
    salesline sl
    on s.Sale_ID = sl.Sale_ID
    where DATE_FORMAT(s.Date,'%m') = 4
    and DATE_FORMAT(s.Date,'%Y') = 2014
    group by sl.Product_ID
    order by Sales desc
    limit 10

这将为您提供 2014 年 4 月最畅销的 10 种产品。

【讨论】:

    【解决方案2】:

    试试这个:

    如果您想通过销售数量来查看:

    select product_id, count(product_id)
    from salesline
    where sale_id in (select sale_id from sales where date BETWEEN '2013-04-01' AND '2013-04-30')
    group by product_id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 2020-03-06
      • 2021-11-28
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      相关资源
      最近更新 更多