日期:2014-05-18 浏览次数:20704 次
select PCode, Currency ,Rate=(select top 1 Rate from A where Code=B.Code and StartDate<EnabledDate order by StartDate desc) ,PriceRMB=(select top 1 Rate from A where Code=B.Code and StartDate<EnabledDate order by StartDate desc)*Price ,EnabledDate from B
------解决方案--------------------
写法就是这样,随手敲的,不排除手误.
当然,也可以用临时表按startdate和enabledate分别生成identity列,然后用identity列去连表
select b.pcode,b.currency,a.rate,b.price*a.rate priceRMB,b.enabledDate from tB b inner join tA a on (select count(*) from tB x where x.EnabledDate<b.EnabledDate) = (select count(*) from tA x where x.StartDate<a.StartDate)
------解决方案--------------------
select id= identity(int,1,1),* into #tb1 from ta select id= identity(int,1,1),* into #tb2 from tb select b. pcode,b.currency,a.rate,pricermb=(a.rate*b.price),b.enabledate from #tb1 a on #tb2 b on a.code=b.currency
------解决方案--------------------
select B.PCode ,B.Currency,A.Rate ,(A.Rate*B.Price)as PriceRMB,B.EnabledDate from A,B
where A.Currency=B.Currency