我需要使用下面的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
此处当前,BPO和预测应该是特定指数的类型和代码组合的平均值。 我们还消除了与表2不匹配的所有行。我也在表1组合中放入Null值,因为这些是我表中的实际值
以下是我的脚本:
表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')
答案 0 :(得分:0)
以下解决方案满足您的需求
select T2.[index],Avg(T1.[Current]) as [Current] ,avg(t1.[bpo]) as bpo,avg(t1.[Forecast]) as [Forecast]
from Table1 T1
join Table2 T2
on isnull(T1.[type],'') = t2.[type]
and isnull(t1.code,'') = t2.code
group by T2.[index]
答案 1 :(得分:0)
DECLARE c_Report CURSOR FOR
SELECT DISTINCT [Index] FROM Table4
create table #tempb([Index] varchar(1000), CC decimal(25,6),BPO decimal(25,6),ForeCast decimal(25,6))
DECLARE @Index varchar(1000)
OPEN c_Report
FETCH NEXT FROM c_Report INTO @Index
WHILE @@Fetch_Status = 0
BEGIN
insert into #tempb
select [Index]
,Avg([Current]),Avg(BPO),Avg(ForeCast) from
(select distinct @Index as [Index],* from
(Select [Type]
from Table4
where [Index]=@Index) a
,
(Select Code
from Table4
where [Index]=@Index)b) c
join Table3 d
on c.[Type]=d.[Type] and c.Code=d.Code
group by [Index]
FETCH NEXT FROM c_Report INTO @Index
END
close c_Report
deallocate c_Report
select * from #tempb
这就是我想要的。谢谢你的帮助。