我想根据前两列对2个表进行分组。我已经成功地做到了,下面可以看到代码。 我需要一些帮助,以将每个表中的剩余列求和,并使用重命名的列插入到我的输出表中,如下图所示。 在输出中,FORECAST列是MI_STAT_TOTAL表的结果,而SALES列是SALES_TOTAL表的结果。
表:MI_STAT_TOTAL
Planning_Hierarchy_2 Region_Description Measure_Values
xx LATAM 2
yy ASPAC 3
zz EMEA 4
zz EMEA 1
aa ASPAC 1
表:SALES_TOTAL
Product_Number Region Item_Values
xx LATAM 1
yy ASPAC 2
zz EMEA 2
zz EMEA 1
bb USA 2
期望的输出 表格:P2_Region_Unique
Product_Number Region Forecast Sales
xx LATAM 2 1
yy ASPAC 3 2
zz EMEA 5 3
aa ASPAC 1
bb USA 2
下面的代码仅考虑两个表之间的GROUP BY,而不考虑SUM:
CREATE TABLE P2_Region_Unique (
Product_Number VARCHAR(100),
Region VARCHAR(13)
);
INSERT INTO P2_Region_Unique
SELECT s.Planning_Hierarchy_2 AS Product_Number, s.Region_Description` AS Region
FROM MI_STAT_TOTAL s
UNION
SELECT m.`Product Number` AS `Product_Number`, m.`Region` AS Region
FROM SALES_TOTAL m;
考虑到SUM也包含预期的输出表格式
CREATE TABLE P2_Region_Unique (
`Product Number` VARCHAR(100),
`Region` VARCHAR(13),
`Forecast` DECIMAL (20,10),
`Sales` DECIMAL (20,10)
);
答案 0 :(得分:1)
在MySQL中,您可以使用INSERT ... SELECT
syntax将SELECT
变成INSERT
。
样本数据表明您正在处理N-M联接。首先要做的是将聚合移动到子查询中,如下所示:
INSERT INTO P2_Region_Unique
SELECT s.product_number, s.region, f.forecast, s.sales
FROM
(
SELECT product_number, region, SUM(item_values) sales
FROM SALES_TOTAL
GROUP BY product_number, region
) s
LEFT JOIN (
SELECT planning_hierarchy_2 product_number, region_description region, SUM(measure_values) forecast
FROM MI_STAT_TOTAL
GROUP BY planning_hierarchy_2, region_description
) f ON f.product_number = s.product_number AND f.region = s.region
GROUP BY s.product_number, s.region
这将起作用,但不会从连接的两端处返回记录。为此,您可以使用UNIONed查询来模拟FULL OUTER JOIN(如@Nick所指出的,在MySQL中不受支持),例如:
INSERT INTO P2_Region_Unique
SELECT s.product_number, s.region, f.forecast, s.sales
FROM
(
SELECT product_number, region, SUM(item_values) sales
FROM SALES_TOTAL
GROUP BY product_number, region
) s
LEFT JOIN (
SELECT planning_hierarchy_2 product_number, region_description region, SUM(measure_values) forecast
FROM MI_STAT_TOTAL
GROUP BY planning_hierarchy_2, region_description
) f ON f.product_number = s.product_number AND f.region = s.region
GROUP BY s.product_number, s.region
UNION
SELECT f.product_number, f.region, f.forecast, s.sales
FROM
(
SELECT planning_hierarchy_2 product_number, region_description region, SUM(measure_values) forecast
FROM MI_STAT_TOTAL
GROUP BY planning_hierarchy_2, region_description
) f
LEFT JOIN (
SELECT product_number, region, SUM(item_values) sales
FROM SALES_TOTAL
GROUP BY product_number, region
) s ON f.product_number = s.product_number AND f.region = s.region
GROUP BY f.product_number, f.region
UNION
运算符可消除子查询中的重复项。
此 demo on DB Fiddle 产生:
| product_number | region | forecast | sales |
| -------------- | ------ | -------- | ----- |
| aa | ASPAC | 1 | |
| bb | USA | | 2 |
| xx | LATAM | 2 | 1 |
| yy | ASPAC | 3 | 2 |
| zz | EMEA | 5 | 3 |
答案 1 :(得分:0)
尝试一下
select
s.ProductNumber,
s.Region,
sum(measureValues) Forecast,
sum(itemValues) Sales
from MI_STAT_TOTAL m
FULL OUTER join SALES_TOTAL s
on m.PlanningHierarcy2 = s.ProductNumber
group by
s.ProductNumber,
s.Region
用于插入操作
INSERT INTO P2_Region_Unique
select
s.ProductNumber,
s.Region,
sum(measureValues) Forecast,
sum(itemValues) Sales
from MI_STAT_TOTAL m
FULL OUTER join SALES_TOTAL s
on m.PlanningHierarcy2 = s.ProductNumber
group by
s.ProductNumber,
s.Region
如果您的MySQL版本低于版本8,则
select
s.ProductNumber,
s.Region,
sum(measureValues) Forecast,
sum(itemValues) Sales
from
(
SELECT * from MI_STAT_TOTAL m
LEFT join SALES_TOTAL s
on m.PlanningHierarcy2 = s.ProductNumber
union
SELECT * from MI_STAT_TOTAL m
RIGHT JOIN SALES_TOTAL s
on m.PlanningHierarcy2 = s.ProductNumber
)
group by
s.ProductNumber,
s.Region