我做了下面的示例代码作为我所面临的一个例子。
让我们说一个有这张桌子: 股票:我每天都有我当前的股票图片。 ProductType:如果我的库存中的产品是水果,我在哪里可以获得信息。 FruitePrices:我有每个水果价格的历史价格。 OthersPrices:我有其他类型产品的历史价格。
我需要一个给定的库存日期加载所有历史价格,无论是水果还是其他。
所以首先我需要在STOCK和ProductType之间进行Join以从ProductType返回列IsFruit(位列)。我只知道这样做是为了给股票日。
然后我需要为 My above join 返回的这个产品建立一个历史价格表。如果 IsFruit = 1从FruitePrice表中获取价格,如果 = 0从OthersPrice表中获取。
我面临的问题是,假设我正在 15 年 4 月 10 日在 Stock 上寻找我的产品,并在STOCK和ProductType之间进行我的第一次连接,我返回:
StockDate Product IsFrute
10Apr15 Banana 1
10Apr15 Milk 0
然后我进行第二次加入,从 1015 年 1 月 10 日到 2015 年 4 月 10 日从各自的表中返回香蕉和牛奶价格。
我希望这样看香蕉和牛奶的历史价格:
StockDate PriceDate Product Price
10Apr15 10Jan15 Banana NULL
10Apr15 10Jan15 Milk 4
10Apr15 11Jan15 Banana 7
10Apr15 11Jan15 Milk 11
10Apr15 ... Banana 8
10Apr15 ... Milk 3
10Apr15 10Apr15 Banana 5
10Apr15 10Apr15 Milk 2
我可能没有错过产品历史价格的日期,这种情况应该返回NULL。上面的例子香蕉从 11Jan15 开始有价格,然后应该在 10Jan15 返回 NULL
最后,我遇到的问题是,我在上表中为每个组合在日期上有一行,例如 nxnx n。
例如我在做什么:
Select GP.PriceDate, GP.ProdName, case(GP.IsFrute = 1, FP.Price, else OP.Price)
(
Select STK.PriceDate, STK.ProdName, PT.IsFrute
from Stock as STK
Join ProductType as PT on PT.ProdName = STK.ProdName
) as GP
join FruitPrice as FP on FP.Name = GP.ProdName
join OthersPrice as OT on OP.Name = GP.Name
Where GP.PriceDate = '10Apr15'
and FP.Date >= '10Jan15' and FP.Date <= '10Apr15'
and OT.Date >= '10Jan15' and OT.Date <= '10Apr15'
我的真实数据,分解每个表的日期以显示正在发生的事情:
Refdate Date Date Price
2015-05-13 2015-05-04 NULL 2650.000000000000
2015-05-13 2015-05-05 NULL 2650.000000000000
2015-05-13 2015-05-06 NULL 2650.000000000000
2015-05-13 2015-05-07 NULL 2460.000000000000
2015-05-13 2015-05-08 NULL 2490.000000000000
2015-05-13 2015-05-11 NULL 2660.000000000000
2015-05-13 2015-05-12 NULL 2660.000000000000
2015-05-13 2015-05-13 NULL 2770.000000000000
2015-05-13 2015-05-14 NULL 2610.000000000000
2015-05-13 2014-12-31 2015-05-06 1490.000000000000
2015-05-13 2014-12-31 2015-05-07 1490.000000000000
2015-05-13 2014-12-31 2015-05-08 1490.000000000000
2015-05-13 2014-12-31 2015-05-11 1490.000000000000
2015-05-13 2014-12-31 2015-05-12 1490.000000000000
2015-05-13 2014-12-31 2015-05-13 1490.000000000000
2015-05-13 2014-12-31 2015-05-14 1490.000000000000
2015-05-13 2014-12-31 2015-05-05 1490.000000000000
2015-05-13 2015-01-02 2015-05-06 1490.000000000000
2015-05-13 2015-01-02 2015-05-07 1490.000000000000
2015-05-13 2015-01-02 2015-05-08 1490.000000000000
2015-05-13 2015-01-02 2015-05-11 1490.000000000000
2015-05-13 2015-01-02 2015-05-12 1490.000000000000
更新 2: 真实查询
With cte_RiskFactors(RiskFactor)
AS
(Select RiskFactor, ProdId, from Exposure where Refdate = '20150513')
Select Expo.refdate, expo.riskfactor, expo.ProdId, px.price
from cte_RiskFactors as cte
join Exposure as expo on cte.RiskFactor = expo.RiskFactor
join Prices as px on px.Id_Product = cte.ProdId
where px.[Date] >= '20150501' and px.[Date] <= '20150513'
UNION
Select Expo.refdate, expo.riskfactor, iv.Value
from cte_RiskFactors as cte
join Exposure as expo on cte.RiskFactor = expo.RiskFactor
join IndexesValue as iv on iv.Id_RiskFactor = cte.IdRF
where iv.[Date] >= '20150501' and iv.[Date] <= '20150513'
错误:
Msg 207, Level 16, State 1, Line 62
Invalid column name 'IdRF'.
Msg 207, Level 16, State 1, Line 59
Invalid column name 'price'.