我需要使用以下 2 个表并将最后一个表作为我的输出。任何帮助都会很棒。
表格1 :
Type Code Current BPO Forecast
NULL NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL
A NULL NULL NULL NULL
A NULL NULL NULL
A A001 55 32 56.322
A A002 NULL NULL NULL
A A003 66.22 44.21 81.13
A A004 NULL NULL NULL
A B001 NULL NULL NULL
A B002 NULL NULL NULL
R A001 29.423 53.245 82.444
R A003 100 100 100
R A004 NULL NULL NULL
R A023 NULL NULL NULL
R C001 89.333 23.66 100
S A001 22.21 71.1 38.45
S C001 67 34 62
S NULL NULL NULL
Z A003 12 22.1 90.42
Z B001 56 43.332 80.33
Z A023 NULL NULL NULL
表 2:
Index Type Code
Bact A A001
Bact R A001
Bact Z B001
Bact S C001
Dis1 A A001
Dis1 A A002
Dis1 A A003
Dis1 R C001
Dis1 Z A023
MEX A A004
MEX A B001
MEX R A001
MEX S A001
MEX A B002
TED R A003
TED R A004
TED S A001
TED Z A003
ZES R A023
ZES Z A003
ZES Z A023
ZES Z B001
我需要以下列作为我的输出:
Index Current BPO Forecast
Bact (Avg of A A001,A B001,A C001,R A001,R B001,R C001,S A001,S B001,S C001,Z A001,Z B001,Z C001)
Dis1
MEX
TED
ZES
此处 Current 、 BPO 和 Forecast 应该是特定索引的类型和代码组合的平均值。我们还消除了与表 2 不匹配的所有行。我还将 Null 值放入表 1 组合中,因为这些是我表中的实际值
下面还有我的脚本:
表 1 创建和插入脚本:-
CREATE TABLE [dbo].[Table1](
[Type] [char](1) NULL,
[Code] [varchar](5) NULL,
[Current] [decimal](18, 8) NULL,
[BPO] [decimal](18, 8) NULL,
[Forecast] [decimal](18, 8) NULL
) ON [PRIMARY]
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES(NULL,NULL,NULL,NULL,NULL)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('',NULL,NULL,NULL,NULL)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES(NULL,'',NULL,NULL,NULL)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('','',NULL,NULL,NULL)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('A',NULL,NULL,NULL,NULL)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('A','',NULL,NULL,NULL)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('A','A001',55.00000000,32.00000000,56.32200000)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('A','A002',NULL,NULL,NULL)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('A','A003',66.22000000,44.21000000,81.13000000)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('A','A004',NULL,NULL,NULL)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('A','B001',NULL,NULL,NULL)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('A','B002',NULL,NULL,NULL)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('R','A001',28.42300000,53.24500000,82.44400000)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('R','A003',100.00000000,100.00000000,100.00000000)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('R','A004',NULL,NULL,NULL)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('R','A023',NULL,NULL,NULL)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('R','C001',89.34440000,52.43200000,100.00000000)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('S','A001',22.22000000,18.32200000,65.00000000)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('S','C001',67.00000000,34.00000000,62.00000000)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('S','',NULL,NULL,NULL)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('Z','A003',12.00000000,22.56000000,38.43000000)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('Z','B001',56.00000000,43.23300000,81.69000000)
INSERT INTO [Table1] ([Type],[Code],[Current],[BPO],[Forecast])VALUES('Z','A023',NULL,NULL,NULL)
表 2 创建和插入脚本:-
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('Bact','A','A001')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('Bact','R','A001')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('Bact','Z','B001')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('Bact','S','C001')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('Dis 1','A','A001')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('Dis 1','A','A002')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('Dis 1','A','A003')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('Dis 1','R','C001')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('Dis 1','Z','A023')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('MEX','A','A004')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('MEX','A','B001')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('MEX','R','A001')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('MEX','S','A001')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('MEX','A','B002')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('TED','R','A003')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('TED','R','A004')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('TED','S','A001')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('TED','Z','A003')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('ZES','R','A023')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('ZES','Z','A003')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('ZES','Z','A023')
INSERT INTO [Table2] ([Index],[Type],[Code])VALUES('ZES','Z','B001')