【问题标题】:Using limit in group by在 group by 中使用限制
【发布时间】:2018-06-20 12:28:49
【问题描述】:

这是我的桌子。

Financials:
    Date
    CountryID
    ProductID
    Revenue
    Cost

我需要找到每个国家/地区按收入排名前 5 位的产品。有些产品会列出不止一次,所以我需要汇总每个产品的收入。

SELECT
  Financials.CountryID,
  Financials.ProductID,
  SUM(Financials.Revenue) AS total
FROM Financials
INNER JOIN (SELECT
  CountryID,
  GROUP_CONCAT (ProductID ORDER BY Revenue DESC) grouped_ID
FROM Financials
GROUP BY CountryID) group_max
  ON Financials.CountryID = group_max.CountryID
  AND FIND_IN_SET(ProductID, grouped_ID) BETWEEN 1 AND 6

GROUP BY Financials.ProductID
ORDER BY Financials.CountryID, total DESC

这是我目前所拥有的。它不工作。帮忙?

编辑

我一直在使用https://sqltest.net/。请参阅下面我使用的插入命令

CREATE TABLE mysql_test ( 
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
CountryID VARCHAR(30) NOT NULL, 
ProductID VARCHAR(30) NOT NULL,  
Revenue VARCHAR(50), 
cost VARCHAR(50), 
reg_date TIMESTAMP 
); 

CREATE TABLE mysql_test_sql ( 
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
CountryID VARCHAR(30) NOT NULL, 
ProductID VARCHAR(30) NOT NULL,  
Revenue VARCHAR(50), 
cost VARCHAR(50), 
reg_date TIMESTAMP 
); 

INSERT INTO `mysql_test` (`id`, `CountryID`, `ProductID`, `Revenue`, `cost`, `reg_date`) VALUES ('1', 'Canada', 'Doe', '20', '5', '2010-01-31 12:01:01'); 
INSERT INTO `mysql_test` (`id`, `CountryID`, `ProductID`, `Revenue`, `cost`, `reg_date`) VALUES ('2', 'USA', 'Tyson', '40', '15', '2010-02-14 12:01:01'); 
INSERT INTO `mysql_test` (`id`, `CountryID`, `ProductID`, `Revenue`, `cost`, `reg_date`) VALUES ('3', 'France', 'Keaton', '80', '25', '2010-03-25 12:01:01'); 
INSERT INTO `mysql_test` (`id`, `CountryID`, `ProductID`, `Revenue`, `cost`, `reg_date`) VALUES ('4', 'France', 'Joe', '180', '45', '2010-04-25 12:01:01'); 
INSERT INTO `mysql_test` (`id`, `CountryID`, `ProductID`, `Revenue`, `cost`, `reg_date`) VALUES ('5', 'France', 'Bill', '30', '6', '2010-04-25 12:01:01'); 
INSERT INTO `mysql_test` (`id`, `CountryID`, `ProductID`, `Revenue`, `cost`, `reg_date`) VALUES ('6', 'France', 'Emma', '15', '2', '2010-04-25 12:01:01'); 
INSERT INTO `mysql_test` (`id`, `CountryID`, `ProductID`, `Revenue`, `cost`, `reg_date`) VALUES ('7', 'France', 'Joe', '60', '36', '2010-04-25 12:01:01'); 
INSERT INTO `mysql_test` (`id`, `CountryID`, `ProductID`, `Revenue`, `cost`, `reg_date`) VALUES ('8', 'France', 'Jammer', '130', '26', '2010-04-25 12:01:01'); 

INSERT INTO `mysql_test` (`id`, `CountryID`, `ProductID`, `Revenue`, `cost`, `reg_date`) VALUES ('9', 'France', 'Louis', '350', '12', '2010-04-25 12:01:01'); 
INSERT INTO `mysql_test` (`id`, `CountryID`, `ProductID`, `Revenue`, `cost`, `reg_date`) VALUES ('10', 'France', 'dennis', '100', '175', '2010-04-25 12:01:01'); 
INSERT INTO `mysql_test` (`id`, `CountryID`, `ProductID`, `Revenue`, `cost`, `reg_date`) VALUES ('11', 'USA', 'zooey', '70', '16', '2010-04-25 12:01:01'); 
INSERT INTO `mysql_test` (`id`, `CountryID`, `ProductID`, `Revenue`, `cost`, `reg_date`) VALUES ('12', 'France', 'Alex', '2', '16', '2010-04-25 12:01:01'); 

【问题讨论】:

  • 您使用的是 MySQL 还是 MS SQL Server?
  • 请提供一些示例数据
  • 我假设 OP 使用 mysql。因为 mysql 可以使用 group concat 功能。
  • “sql-server-group-concat”标签应该被编辑。因为它会让人感到困惑。

标签: mysql sql group-by group-concat


【解决方案1】:

这是一种方法:

select countryId,
       substring_index(group_concat(productId order by revenue desc), ',', 5) as top5
from (select countryId, productId, sum(revenue) as revenue
      from mysql_test
      group by countryId, productId
     ) cp
group by countryId;

注意:group_concat() 的中间结果的默认内部限制约为 1028 个字符。这应该适用于每个国家/地区的数百种产品。默认长度是系统设置,可以更改。

【讨论】:

    猜你喜欢
    • 2014-07-02
    • 2017-10-28
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    相关资源
    最近更新 更多