我有一个每天从外部数据源更新的数据集(示例)。然后将该数据与一些其他内部数据(Scale)组合并形成新表。
示例包含一些有时可能有误的数字。我在一个单独的表(错误)中注册数据不正确的资金和日期。我希望从另一个我知道正确的日期获取数据并将其用作代理。
我已经制作了一小段代码来说明我的问题。所以 - 目的是最终得到一个完整的表格,其中包含所有单一投资的历史数据(即,我通过投资基金查看)不同公司的投资组合。如果基金的任何数据已经交付有错误,基金的数据应该从另一个日期(不一定是之前的日期)获取。即使不正确的数据被另一个日期的数据替换,缩放也应保持不变。
我已经给了它很多想法,发现我似乎找到解决方案的唯一方法是给UNION两个不同的选择,一个用原始数据排除数据不正确的资金,另一个用替换数据。但我觉得应该有一种更简单的方法来实现我的需要。我的原始数据都在表格和视图中,并且现在很大,而且有点慢,所以我有兴趣找到创建新表的最有效方法。
提前致谢! 线
CREATE TABLE Example(
MarketDate datetime NOT NULL,
FundName VARCHAR(20) NOT NULL,
SecurityName VARCHAR(48) NOT NULL,
MarketValue FLOAT(25),
Risk FLOAT(25),
);
CREATE TABLE tblScale(
MarketDate datetime NOT NULL,
Entity VARCHAR (20) NOT NULL,
FundName VARCHAR(48) NOT NULL,
Scale FLOAT(25),
);
CREATE TABLE Errors(
MarketDate datetime NOT NULL,
RiskDate datetime NOT NULL,
FundName VARCHAR(48) NOT NULL,
);
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-15', 'Fund1', 'Bond1', 2000.00, 5 );
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-15', 'Fund1', 'Bond2', 1500.00, 4);
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-15', 'Fund1', 'Bond3', 1300.00, 3 );
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-15', 'Fund1', 'Bond4', 300.00, 109 );
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-15', 'Fund1', 'Bond5', 700.00, 400 );
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-15', 'Fund1', 'Bond6', 600.00, 350 );
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-14', 'Fund1', 'Bond1', 2100.00, 5.1 );
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-14', 'Fund1', 'Bond2', 1400.00, 4.2 );
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-14', 'Fund1', 'Bond3', 1330.00, 3.9 );
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-14', 'Fund1', 'Bond4', 200.00, 2.1 );
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-14', 'Fund1', 'Bond5', 400.00, 2.5 );
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-14', 'Fund1', 'Bond6', 500.00, 2.6 );
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-15', 'Fund2', 'Bond7', 1800.00, 3.5 );
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-15', 'Fund2', 'Bond8', 1900.00, 4.5);
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-15', 'Fund2', 'Bond9', 1300.00, 3 );
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-15', 'Fund2', 'Bond10', 350.00, 2.1 );
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-14', 'Fund2', 'Bond7', 1700.00, 3.4 );
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-14', 'Fund2', 'Bond8', 1810.00, 4.2 );
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-14', 'Fund2', 'Bond9', 1330.00, 3.4 );
INSERT INTO Example (MarketDate,FundName,SecurityName,MarketValue,Risk) VALUES ('2015-06-14', 'Fund2', 'Bond10', 320.00, 2.0 );
INSERT INTO tblScale (MarketDate,Entity,FundName,Scale) VALUES ('2015-06-15', 'Comp1', 'Fund1', 0.76 );
INSERT INTO tblScale (MarketDate,Entity,FundName,Scale) VALUES ('2015-06-15', 'Comp2', 'Fund1', 0.10 );
INSERT INTO tblScale (MarketDate,Entity,FundName,Scale) VALUES ('2015-06-15', 'Comp3', 'Fund1', 0.14 );
INSERT INTO tblScale (MarketDate,Entity,FundName,Scale) VALUES ('2015-06-15', 'Comp1', 'Fund2', 0.30 );
INSERT INTO tblScale (MarketDate,Entity,FundName,Scale) VALUES ('2015-06-15', 'Comp2', 'Fund2', 0.35 );
INSERT INTO tblScale (MarketDate,Entity,FundName,Scale) VALUES ('2015-06-15', 'Comp3', 'Fund2', 0.25 );
INSERT INTO tblScale (MarketDate,Entity,FundName,Scale) VALUES ('2015-06-14', 'Comp1', 'Fund1', 0.75 );
INSERT INTO tblScale (MarketDate,Entity,FundName,Scale) VALUES ('2015-06-14', 'Comp2', 'Fund1', 0.10 );
INSERT INTO tblScale (MarketDate,Entity,FundName,Scale) VALUES ('2015-06-14', 'Comp3', 'Fund1', 0.15 );
INSERT INTO tblScale (MarketDate,Entity,FundName,Scale) VALUES ('2015-06-14', 'Comp1', 'Fund2', 0.30 );
INSERT INTO tblScale (MarketDate,Entity,FundName,Scale) VALUES ('2015-06-14', 'Comp2', 'Fund2', 0.35 );
INSERT INTO tblScale (MarketDate,Entity,FundName,Scale) VALUES ('2015-06-14', 'Comp3', 'Fund2', 0.25 );
INSERT INTO Errors (MarketDate,RiskDate,FundName) VALUES ('2015-06-15', '2015-06-14', 'Fund1' );
Declare @Todate as datetime = '2015-06-15';
select data.MarketDate as MarketDate
,data.MarketDate as RiskDate
,scale.Entity
,data.SecurityName
,sum(data.MarketValue)*scale.Scale as MV
from Example data
inner join tblScale scale
on data.MarketDate=scale.MarketDate
and data.FundName=scale.FundName
where data.MarketDate=@Todate
and data.FundName not in (select FundName from Errors where MarketDate=@Todate)
group by data.MarketDate,scale.Entity,data.SecurityName,scale.Scale
union
select @Todate as MarketDate
,data.MarketDate as RiskDate
,scale.Entity
,data.SecurityName
,sum(data.MarketValue)*scale.Scale as MV
from (select @Todate as Today,* from Example
where fundname in (select fundname from Errors where marketdate=@todate)
and marketdate in (select riskdate from Errors where marketdate=@todate)
) data
inner join tblScale scale
on data.FundName=scale.FundName
and data.Today=scale.MarketDate
where scale.MarketDate=@Todate
group by data.MarketDate
,scale.Entity
,data.SecurityName
,scale.Scale
,scale.MarketDate
答案 0 :(得分:0)
首先,您可以使用UNION ALL
,这将省略重复检查。这会快得多。
另一个建议是第一个IN()
中的SELECT
。如果子查询返回许多行,这可能会导致长运行时。
我建议从中重新设计:
select data.MarketDate as MarketDate
,data.MarketDate as RiskDate
,scale.Entity
,data.SecurityName
,sum(data.MarketValue)*scale.Scale as MV
from Example data
INNER join tblScale scale
on data.MarketDate=scale.MarketDate
and data.FundName=scale.FundName
where data.MarketDate=@Todate
and data.FundName not in (select FundName from Errors where MarketDate=@Todate)
group by data.MarketDate,scale.Entity,data.SecurityName,scale.Scale
此LEFT JOIN
包括IS NULL
过滤器:
select data.MarketDate as MarketDate
,data.MarketDate as RiskDate
,scale.Entity
,data.SecurityName
,sum(data.MarketValue)*scale.Scale as MV
from Example data
inner join tblScale scale
on data.MarketDate=scale.MarketDate
and data.FundName=scale.FundName
LEFT JOIN (select FundName from Errors where MarketDate=@Todate) as fundname
ON data.FundName = fundname.FundName
where data.MarketDate=@Todate
and fundname.Fundname IS NULL
group by data.MarketDate,scale.Entity,data.SecurityName,scale.Scale