0

以下の 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 は、特定のインデックスの Type と Code の組み合わせの平均である必要があります。また、表 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')
4

2 に答える 2

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

これが私が探していたものでした。お手伝いありがとう。

于 2013-07-02T15:02:57.360 に答える
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]
于 2013-06-29T06:07:52.067 に答える