基于SQL中的一列的聚集行

发布于 2025-01-27 09:10:12 字数 3877 浏览 3 评论 0原文

我有一个看起来像这样的数据集:

report_idcategory_idproduct_idyear_monthtotal_sales
10a120220110
10A120220216
10A220220111
10A32022018
10A410 A 4 202202202201 12
10A4202202202202 15
10B719 192202 19 192202 19 19220219
10B820220417
10B92022039

我试图汇总该表,如果category_id = a没有任何category_id = b; aCTORY_ID = A的所有产品都可以称为“ MISC”。

所需的数据集应该看起来像:

report_idcategory_idproduct_idyear_monthtotal_sales_sales
10amisc20220141
10amisc20220231
10B720220219
10B820220417
10B92022039

我完全丢失了如何生产此数据的数据放。

感谢您的帮助

I have a data set that looks like this:

report_idcategory_idproduct_idyear_monthtotal_sales
10A120220110
10A120220216
10A220220111
10A32022018
10A420220112
10A420220215
10B720220219
10B820220417
10B92022039

I am trying to summarize the table where I can aggregate by year_month if category_id = A without any aggregation for category_id = B; all products for category_id = A can be referred to as 'misc'.

The desired data set should look like this:

report_idcategory_idproduct_idyear_monthtotal_sales
10Amisc20220141
10Amisc20220231
10B720220219
10B820220417
10B92022039

I am totally lost as to how to produce this data set.

Thanks for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哆啦不做梦 2025-02-03 09:10:13

您可以汇总:

SELECT report_id,
       category_id,
       IF(GROUP_CONCAT(product_id) LIKE '%,%', 
          'misc', 
          GROUP_CONCAT(product_id))            AS product_id,
       year_month_,
       SUM(total_sales)                        AS total_sales
FROM tab
GROUP BY report_id,
         category_id,
         year_month_

检查demo 在这里

注意:这是一个通用的解决方案,如果您的“杂项”值应在任何类别中找到,而不是根据您的特定示例数据找到唯一的“ A”类别。

You can aggregate over:

  • total_sales with the SUM function
  • product_id with the GROUP_CONCAT in a conditional way using the IF function
SELECT report_id,
       category_id,
       IF(GROUP_CONCAT(product_id) LIKE '%,%', 
          'misc', 
          GROUP_CONCAT(product_id))            AS product_id,
       year_month_,
       SUM(total_sales)                        AS total_sales
FROM tab
GROUP BY report_id,
         category_id,
         year_month_

Check the demo here.

Note: this is a generalized solution, in case your 'misc' value should ever be found in any category, rather than the only category 'A', as per your specific sample data.

呢古 2025-02-03 09:10:12

我们可以使用案例根据category_id中的值将列product_id格式化,然后按结果进行分组。

 创建表mytable(
  report_id int,
  aCTORY_ID char(1),
  product_id int,
  年varchar(6),
  total_sales int
  );
 
 插入Mytable值
(10,'a',1,202201,10),
(10,'a',1,202202,16),
(10,'a',2,202201,11),
(10,'a',3,202201,8),
(10,'a',4,202201,12),
(10,'a',4,202202,15),
(10,'b',7,202202,19),
(10,'b',8,202204,17),
(10,'b',9,202203,9);
 
 选择
  report_id,
  category_id,
  当category_id ='a'then's'misc'的情况
  否则product_id end product_id,
  年度,
  sum(total_sales)total_sales
来自mytable
组为1,2,3,4;
 
 report_id | category_id | product_id |年| total_sales
--------:| :----------- | :---------- | :-------- | -----------:
       10 | A | MISC | 202201 | 41
       10 | A | MISC | 202202 | 31
       10 | b | 7 | 202202 | 19
       10 | b | 8 | 202204 | 17
       10 | b | 9 | 202203 | 9

db<>>

We can use case to format the column product_id according to the value in category_id and then group by the result.

create table myTable(
  Report_id int,
  Category_id char(1),
  Product_id int,
  Yearmonth varchar(6),
  total_sales int
  );
insert into myTable values
(10   ,'A',   1,  202201  ,10),
(10   ,'A',   1,  202202  ,16),
(10   ,'A',   2,  202201  ,11),
(10   ,'A',   3,  202201  ,8),
(10   ,'A',   4,  202201  ,12),
(10   ,'A',   4,  202202  ,15),
(10   ,'B',   7,  202202  ,19),
(10   ,'B',   8,  202204  ,17),
(10   ,'B',   9,  202203  ,9);
Select
  Report_id,
  Category_id,
  Case when category_id = 'A' then 'misc'
  Else product_id end Product_ID,
  Yearmonth,
  Sum(total_sales) total_sales
From myTable
Group by 1,2,3,4;
Report_id | Category_id | Product_ID | Yearmonth | total_sales
--------: | :---------- | :--------- | :-------- | ----------:
       10 | A           | misc       | 202201    |          41
       10 | A           | misc       | 202202    |          31
       10 | B           | 7          | 202202    |          19
       10 | B           | 8          | 202204    |          17
       10 | B           | 9          | 202203    |           9

db<>fiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文