简体   繁体   English

SQL 查询 - 每个类别、每个季度最畅销的产品

[英]SQL query - highest selling product, per category, per quarter

I'm trying to write a query which will show the highest selling products, per category, per quarter.我正在尝试编写一个查询,该查询将显示每个类别、每个季度的最高销量产品。

The 'Product_Orders' table in the query has a field called 'Quantity' also, which will hold the number of orders, and I want to use this in the query to show the highest selling products per category, per quarter, so there would be a Quantity field in the final result of the query below, which I haven't added yet.查询中的“Product_Orders”表也有一个名为“Quantity”的字段,该字段将保存订单数量,我想在查询中使用它来显示每个类别、每个季度的最高销量产品,因此会有下面查询的最终结果中的 Quantity 字段,我尚未添加。

This is basically my query at the moment:这基本上是我目前的查询:

SELECT pc.category_name, pr.product_name, QUARTER(o.order_date) AS QUARTER
FROM Product pr
INNER JOIN Product_Category pc ON pr.category_code = pc.category_code
INNER JOIN Product_Orders po ON po.product_id = pr.product_id
INNER JOIN `Order` o ON o.order_id = po.order_id
WHERE pc.category_name IN ('Game','Console','Accessory')
AND o.order_date BETWEEN '2020-01-01' AND '2020-12-30'
Order BY o.order_date

As per the screenshot below, this obviously isn't working yet as intended:根据下面的屏幕截图,这显然没有按预期工作:

在此处输入图片说明

I'm just wondering how do I go about writing this query so that it shows the highest selling for each category and for each quarter eg Q1,Q2,Q3,Q4 for Game, Q1,Q2,Q3,Q4 for Console我只是想知道如何编写此查询,以便它显示每个类别和每个季度的最高销量,例如游戏的 Q1、Q2、Q3、Q4,游戏机的 Q1、Q2、Q3、Q4

CREATE TABLE syntax for tables used:使用的表的 CREATE TABLE 语法:

CREATE TABLE `Product_Category` (
  `category_code` int(11) NOT NULL AUTO_INCREMENT,
  `category_name` varchar(50) NOT NULL,
  `category_description` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`category_code`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

CREATE TABLE `Product` (
  `product_id` int(11) NOT NULL AUTO_INCREMENT,
  `product_name` varchar(50) NOT NULL,
  `product_description` varchar(255) DEFAULT NULL,
  `retail_price` decimal(10,0) NOT NULL,
  `category_code` int(11) NOT NULL,
  PRIMARY KEY (`product_id`),
  KEY `category_code` (`category_code`),
  CONSTRAINT `Product_ibfk_1` FOREIGN KEY (`category_code`) REFERENCES `Product_Category` (`category_code`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

CREATE TABLE `Order` (
  `order_id` int(11) NOT NULL AUTO_INCREMENT,
  `order_date` date NOT NULL,
  `transaction_amount` decimal(10,0) NOT NULL,
  `shipping_address_id` int(11) NOT NULL,
  `billing_address_id` int(11) NOT NULL,
  `customer_id` int(11) NOT NULL,
  PRIMARY KEY (`order_id`),
  KEY `shipping_address_id` (`shipping_address_id`),
  KEY `billing_address_id` (`billing_address_id`),
  KEY `customer_id` (`customer_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

CREATE TABLE `Product_Orders` (
  `order_id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `quantity` int(11) NOT NULL,
  `selling_price` decimal(10,0) NOT NULL,
  PRIMARY KEY (`order_id`,`product_id`),
  KEY `product_id` (`product_id`),
  CONSTRAINT `Product_Orders_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `Order` (`order_id`),
  CONSTRAINT `Product_Orders_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `Product` (`product_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

INSERTS for tables:表的插入:

INSERT INTO `Product_Category` VALUES 
(1, 'Game', 'Disc-based and cartridge-based media'),
(2, 'Console', 'Game platforms such as Nintento, Playstation and Xbox');

INSERT INTO `Product` VALUES 
(1, 'Metal Gear Solid', 'Game developed by Kojima Productions', 50, 1),
(2, 'Nintendo Switch', 'Nintendo games console', 250, 2),
(3, 'Tekken 7', 'Fighting game developed by NAMCO', 50, 1),
(4, 'Zelda', 'Nintendo Game', 70, 1);

INSERT INTO `Order` VALUES 
(1, '2020-10-10', 50, 1, 1, 1),(2, '2020-01-20', 250, 1, 1, 1),
(3, '2020-01-05', 40, 1, 1, 1),(4, '2020-01-24', 70, 1, 1, 1),
(5, '2020-01-06', 50, 1, 1, 1);

INSERT INTO `Product_Orders` VALUES 
(1, 1, 1, 30),(2, 2, 1, 220),
(3, 3, 1, 25),(4, 4, 1, 70),(5, 1, 5, 50);

Perhaps, like this:也许,像这样:

SELECT pc.category_name, pr.product_name, QUARTER(o.order_date) AS QUARTER, po.Quantity
  FROM Product pr
  INNER JOIN Product_Category pc ON pr.category_code = pc.category_code
  INNER JOIN Product_Orders po ON po.product_id = pr.product_id
  INNER JOIN `Order` o ON o.order_id = po.order_id
 WHERE pc.category_name IN ('Game','Console','Accessory')
  AND o.order_date BETWEEN '2020-01-01' AND '2020-12-30'
Order BY category_name, QUARTER(order_date), Quantity DESC

I've added po.quantity in SELECT and change the ORDER BY code.我在SELECT添加了po.quantity并更改了ORDER BY代码。 One thing I notice you're doing AND o.order_date BETWEEN '2020-01-01' AND '2020-12-30' , isn't it suppose to be until '2020-12-31' ?我注意到你在做AND o.order_date BETWEEN '2020-01-01' AND '2020-12-30'一件事,是不是应该直到'2020-12-31' Also, there's a few variation of filtering date you can do other than BETWEEN .此外,除了BETWEEN之外,您还可以做一些过滤日期的变化。 If you're only looking for year 2020 data, you can simply do AND o.order_date LIKE '2020%' or AND YEAR(o.order_date)='2020' but as far as I know, the latter (using YEAR() ) doesn't make use of (if there's an) index on o.order_date column.如果您只查找 2020 年的数据,您可以简单地执行AND o.order_date LIKE '2020%'AND YEAR(o.order_date)='2020'但据我所知,后者(使用YEAR() ) 不使用(如果有) o.order_date列上的索引。

Here's a demo fiddle inclusive of a few other variation of queries.这是一个演示小提琴,包括一些其他的查询变体。

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

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