如何合并两个满足最小值的字段的表?
How to merge two tables fullfilling a field with his minimum value?
我正在尝试完善我的数据,但找不到好的解决方案。
我正在处理动态生成的周期(示例投影)。
我不知道重新创建 table 只是为了加入我的 table TREND 是否更好。
-- SQL CODE TO RECREATE MY TREND TABLE
CREATE OR REPLACE TABLE test.TREND(
Asset STRING,
Metric_Code STRING,
Metric_V FLOAT64,
Trend FLOAT64,
YearMonth INT64,
YearMonthRank INT64
);
INSERT INTO test.TREND
VALUES ('RDX4', 'AXCP32', 88.0853212, 89.0853212, 202103,1),
('RDX4', 'AXCP32', 88.0853212,85.0853212, 202106,2),
('RDX4', 'AXCP34', 79.41087028,81.0853212, 202103,3),
('RDX4', 'AXCP34', 70.44587,69.0853212, 202106,4),
('MRDX4', 'AXCP32', 88.0853212, 89.0853212, 202103,1),
('MRDX4', 'AXCP32', 88.0853212,85.0853212, 202106,2),
('MRDX4', 'AXCP34', 79.41087028,81.0853212, 202103,3),
('MRDX4', 'AXCP34', 70.44587, 69.0853212,202106,4);
-- SQL CODE TO RECREATE MY PROJECTION TABLE
CREATE OR REPLACE TABLE test.PROJECTION(
Asset STRING,
Metric_Code STRING,
Metric_V FLOAT64,
Trend FLOAT64,
YearMonth INT64,
YearMonthRank INT64
);
INSERT INTO PROJECTION
VALUES (NULL,NULL,NULL,NULL, 202103,1),
(NULL,NULL,NULL,NULL, 202106,2),
(NULL,NULL,NULL,NULL, 202109,3),
(NULL,NULL,NULL,NULL, 202112,4),
(NULL,NULL,NULL,NULL, 202203,5),
(NULL,NULL,NULL,NULL, 202206,6),
(NULL,NULL,NULL,NULL, 202209,7),
(NULL,NULL,NULL,NULL, 202212,8);
现在我卡住了,我无法正确合并数据来计算我的预测。
我试图合并资产,Metric_Code 和趋势,但它并不像你想象的那样有效:
SELECT COALESCE(B.Asset,(SELECT C.asset FROM gopass.TREND C WHERE C.YearMonthRank=1 AND B.asset=C.asset)) AS Asset,
B.Metric_Code,
B.Metric_V,
B.Trend,
A.YearMonth,
A.YearMonthRank
FROM gopass.PROJECTION A
LEFT JOIN gopass.TREND B ON A.YearMonthRank = B.YearMonthRank
ORDER BY Asset ASC;
我不知道如何生成如下输出。
对于每个资产 + Metric_Code 对,我也需要填写 YearMonth 和 YearMonthRank 列。
之后我就可以添加我的预测栏了!
Asset
Metric_Code
Metric_V
Trend
YearMonth
YearMonthRank
RDX4
AXCP32
88.0853212
89.0853212
202103
1
RDX4
AXCP32
88.0853212
85.0853212
202106
2
RDX4
AXCP32
202109
3
RDX4
AXCP32
202112
4
RDX4
AXCP32
202203
5
RDX4
AXCP32
202206
6
RDX4
AXCP32
202209
7
RDX4
AXCP32
202212
8
RDX4
AXCP34
79.41087028
81.0853212
202103
1
RDX4
AXCP34
70.44587
69.0853212
202106
2
RDX4
AXCP34
202109
3
RDX4
AXCP34
202112
4
RDX4
AXCP34
202203
5
RDX4
AXCP34
202206
6
RDX4
AXCP34
202209
7
RDX4
AXCP34
202212
8
MRDX4
AXCP32
88.0853212
89.0853212
202103
1
MRDX4
AXCP32
88.0853212
85.0853212
202106
2
MRDX4
AXCP32
202109
3
MRDX4
AXCP32
202112
4
MRDX4
AXCP32
202203
5
MRDX4
AXCP32
202206
6
MRDX4
AXCP32
202209
7
MRDX4
AXCP32
202212
8
MRDX4
AXCP34
79.41087028
81.0853212
202103
1
MRDX4
AXCP34
70.44587
81.0853212
202106
2
MRDX4
AXCP34
202109
3
MRDX4
AXCP34
202112
4
MRDX4
AXCP34
202203
5
MRDX4
AXCP34
202206
6
MRDX4
AXCP34
202209
7
MRDX4
AXCP34
202212
8
考虑一下:
For each Asset + Metric_Code couple , i need to fill the YearMonth & YearMonthRank columns too
为此,下面的查询首先通过 CROSS JOIN
为每个(Asset
、Metric_Code
)组合扩展 PROJECTION
table。
CREATE TEMP TABLE TREND (
Asset STRING,
Metric_Code STRING,
Metric_V FLOAT64,
Trend FLOAT64,
YearMonth INT64,
YearMonthRank INT64
);
INSERT INTO TREND
VALUES ('RDX4', 'AXCP32', 88.0853212, 89.0853212, 202103,1),
('RDX4', 'AXCP32', 88.0853212,85.0853212, 202106,2),
('RDX4', 'AXCP34', 79.41087028,81.0853212, 202103,3),
('RDX4', 'AXCP34', 70.44587,69.0853212, 202106,4),
('MRDX4', 'AXCP32', 88.0853212, 89.0853212, 202103,1),
('MRDX4', 'AXCP32', 88.0853212,85.0853212, 202106,2),
('MRDX4', 'AXCP34', 79.41087028,81.0853212, 202103,3),
('MRDX4', 'AXCP34', 70.44587, 69.0853212,202106,4)
;
-- SQL CODE TO RECREATE MY PROJECTION TABLE
CREATE TEMP TABLE PROJECTION (
Asset STRING,
Metric_Code STRING,
Metric_V FLOAT64,
Trend FLOAT64,
YearMonth INT64,
YearMonthRank INT64
);
INSERT INTO PROJECTION
VALUES (NULL,NULL,NULL,NULL, 202103,1),
(NULL,NULL,NULL,NULL, 202106,2),
(NULL,NULL,NULL,NULL, 202109,3),
(NULL,NULL,NULL,NULL, 202112,4),
(NULL,NULL,NULL,NULL, 202203,5),
(NULL,NULL,NULL,NULL, 202206,6),
(NULL,NULL,NULL,NULL, 202209,7),
(NULL,NULL,NULL,NULL, 202212,8)
;
SELECT B.Asset_Key AS Asset,
B.Metric_Code_Key AS Metric_Code,
C.Metric_V,
C.Trend,
A.YearMonth,
A.YearMonthRank
FROM PROJECTION A
CROSS JOIN (SELECT DISTINCT Asset AS Asset_Key, Metric_Code AS Metric_Code_Key FROM TREND) B
LEFT JOIN TREND C
ON B.Asset_Key = C.Asset AND B.Metric_Code_Key = C.Metric_Code AND A.YearMonthRank = C.YearMonthRank
ORDER BY 1, 2, A.YearMonthRank;
我正在尝试完善我的数据,但找不到好的解决方案。
我正在处理动态生成的周期(示例投影)。 我不知道重新创建 table 只是为了加入我的 table TREND 是否更好。
-- SQL CODE TO RECREATE MY TREND TABLE
CREATE OR REPLACE TABLE test.TREND(
Asset STRING,
Metric_Code STRING,
Metric_V FLOAT64,
Trend FLOAT64,
YearMonth INT64,
YearMonthRank INT64
);
INSERT INTO test.TREND
VALUES ('RDX4', 'AXCP32', 88.0853212, 89.0853212, 202103,1),
('RDX4', 'AXCP32', 88.0853212,85.0853212, 202106,2),
('RDX4', 'AXCP34', 79.41087028,81.0853212, 202103,3),
('RDX4', 'AXCP34', 70.44587,69.0853212, 202106,4),
('MRDX4', 'AXCP32', 88.0853212, 89.0853212, 202103,1),
('MRDX4', 'AXCP32', 88.0853212,85.0853212, 202106,2),
('MRDX4', 'AXCP34', 79.41087028,81.0853212, 202103,3),
('MRDX4', 'AXCP34', 70.44587, 69.0853212,202106,4);
-- SQL CODE TO RECREATE MY PROJECTION TABLE
CREATE OR REPLACE TABLE test.PROJECTION(
Asset STRING,
Metric_Code STRING,
Metric_V FLOAT64,
Trend FLOAT64,
YearMonth INT64,
YearMonthRank INT64
);
INSERT INTO PROJECTION
VALUES (NULL,NULL,NULL,NULL, 202103,1),
(NULL,NULL,NULL,NULL, 202106,2),
(NULL,NULL,NULL,NULL, 202109,3),
(NULL,NULL,NULL,NULL, 202112,4),
(NULL,NULL,NULL,NULL, 202203,5),
(NULL,NULL,NULL,NULL, 202206,6),
(NULL,NULL,NULL,NULL, 202209,7),
(NULL,NULL,NULL,NULL, 202212,8);
现在我卡住了,我无法正确合并数据来计算我的预测。 我试图合并资产,Metric_Code 和趋势,但它并不像你想象的那样有效:
SELECT COALESCE(B.Asset,(SELECT C.asset FROM gopass.TREND C WHERE C.YearMonthRank=1 AND B.asset=C.asset)) AS Asset,
B.Metric_Code,
B.Metric_V,
B.Trend,
A.YearMonth,
A.YearMonthRank
FROM gopass.PROJECTION A
LEFT JOIN gopass.TREND B ON A.YearMonthRank = B.YearMonthRank
ORDER BY Asset ASC;
我不知道如何生成如下输出。 对于每个资产 + Metric_Code 对,我也需要填写 YearMonth 和 YearMonthRank 列。
之后我就可以添加我的预测栏了!
Asset | Metric_Code | Metric_V | Trend | YearMonth | YearMonthRank |
---|---|---|---|---|---|
RDX4 | AXCP32 | 88.0853212 | 89.0853212 | 202103 | 1 |
RDX4 | AXCP32 | 88.0853212 | 85.0853212 | 202106 | 2 |
RDX4 | AXCP32 | 202109 | 3 | ||
RDX4 | AXCP32 | 202112 | 4 | ||
RDX4 | AXCP32 | 202203 | 5 | ||
RDX4 | AXCP32 | 202206 | 6 | ||
RDX4 | AXCP32 | 202209 | 7 | ||
RDX4 | AXCP32 | 202212 | 8 | ||
RDX4 | AXCP34 | 79.41087028 | 81.0853212 | 202103 | 1 |
RDX4 | AXCP34 | 70.44587 | 69.0853212 | 202106 | 2 |
RDX4 | AXCP34 | 202109 | 3 | ||
RDX4 | AXCP34 | 202112 | 4 | ||
RDX4 | AXCP34 | 202203 | 5 | ||
RDX4 | AXCP34 | 202206 | 6 | ||
RDX4 | AXCP34 | 202209 | 7 | ||
RDX4 | AXCP34 | 202212 | 8 | ||
MRDX4 | AXCP32 | 88.0853212 | 89.0853212 | 202103 | 1 |
MRDX4 | AXCP32 | 88.0853212 | 85.0853212 | 202106 | 2 |
MRDX4 | AXCP32 | 202109 | 3 | ||
MRDX4 | AXCP32 | 202112 | 4 | ||
MRDX4 | AXCP32 | 202203 | 5 | ||
MRDX4 | AXCP32 | 202206 | 6 | ||
MRDX4 | AXCP32 | 202209 | 7 | ||
MRDX4 | AXCP32 | 202212 | 8 | ||
MRDX4 | AXCP34 | 79.41087028 | 81.0853212 | 202103 | 1 |
MRDX4 | AXCP34 | 70.44587 | 81.0853212 | 202106 | 2 |
MRDX4 | AXCP34 | 202109 | 3 | ||
MRDX4 | AXCP34 | 202112 | 4 | ||
MRDX4 | AXCP34 | 202203 | 5 | ||
MRDX4 | AXCP34 | 202206 | 6 | ||
MRDX4 | AXCP34 | 202209 | 7 | ||
MRDX4 | AXCP34 | 202212 | 8 |
考虑一下:
For each Asset + Metric_Code couple , i need to fill the YearMonth & YearMonthRank columns too
为此,下面的查询首先通过 CROSS JOIN
为每个(Asset
、Metric_Code
)组合扩展 PROJECTION
table。
CREATE TEMP TABLE TREND (
Asset STRING,
Metric_Code STRING,
Metric_V FLOAT64,
Trend FLOAT64,
YearMonth INT64,
YearMonthRank INT64
);
INSERT INTO TREND
VALUES ('RDX4', 'AXCP32', 88.0853212, 89.0853212, 202103,1),
('RDX4', 'AXCP32', 88.0853212,85.0853212, 202106,2),
('RDX4', 'AXCP34', 79.41087028,81.0853212, 202103,3),
('RDX4', 'AXCP34', 70.44587,69.0853212, 202106,4),
('MRDX4', 'AXCP32', 88.0853212, 89.0853212, 202103,1),
('MRDX4', 'AXCP32', 88.0853212,85.0853212, 202106,2),
('MRDX4', 'AXCP34', 79.41087028,81.0853212, 202103,3),
('MRDX4', 'AXCP34', 70.44587, 69.0853212,202106,4)
;
-- SQL CODE TO RECREATE MY PROJECTION TABLE
CREATE TEMP TABLE PROJECTION (
Asset STRING,
Metric_Code STRING,
Metric_V FLOAT64,
Trend FLOAT64,
YearMonth INT64,
YearMonthRank INT64
);
INSERT INTO PROJECTION
VALUES (NULL,NULL,NULL,NULL, 202103,1),
(NULL,NULL,NULL,NULL, 202106,2),
(NULL,NULL,NULL,NULL, 202109,3),
(NULL,NULL,NULL,NULL, 202112,4),
(NULL,NULL,NULL,NULL, 202203,5),
(NULL,NULL,NULL,NULL, 202206,6),
(NULL,NULL,NULL,NULL, 202209,7),
(NULL,NULL,NULL,NULL, 202212,8)
;
SELECT B.Asset_Key AS Asset,
B.Metric_Code_Key AS Metric_Code,
C.Metric_V,
C.Trend,
A.YearMonth,
A.YearMonthRank
FROM PROJECTION A
CROSS JOIN (SELECT DISTINCT Asset AS Asset_Key, Metric_Code AS Metric_Code_Key FROM TREND) B
LEFT JOIN TREND C
ON B.Asset_Key = C.Asset AND B.Metric_Code_Key = C.Metric_Code AND A.YearMonthRank = C.YearMonthRank
ORDER BY 1, 2, A.YearMonthRank;