Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Kapital123
Starting Member
31 Posts |
Posted - 2012-12-11 : 05:44:53
|
Hello,I am not sure what the most efficient way to do this is, hence why I have decided to turn to the forum for ideas and advice. Here is Sample Table 1 (4 Columns: Date, Fund, Security and Actual_Weight):Date Fund Security Actual_Weight1/01/2012 FundA Cash 0.21/01/2012 FundA MBS 0.81/01/2012 FundB Cash 0.31/01/2012 FundB MBS 0.72/01/2012 FundA Cash 0.12/01/2012 FundA MBS 0.92/01/2012 FundB Cash 0.22/01/2012 FundB MBS 0.83/01/2012 FundA Cash 0.73/01/2012 FundA MBS 0.33/01/2012 FundB Cash 0.53/01/2012 FundB MBS 0.5Here is Sample Table 2 (4 Columns: Date, Fund, Security and Target_Weight)Date Fund Security Target_Weight1/01/2012 FundA Cash 0.51/01/2012 FundA MBS 0.51/01/2012 FundB Cash 0.71/01/2012 FundB MBS 0.33/01/2012 FundA Cash 0.63/01/2012 FundA MBS 0.43/01/2012 FundB Cash 0.13/01/2012 FundB MBS 0.9Now what I would like to generate through a SELECT statement is this Table (5 Columns) from Table 1 and Table 2 above:Date Fund Security Actual_Weight Target_Weight1/01/2012 FundA Cash 0.2 0.51/01/2012 FundA MBS 0.8 0.51/01/2012 FundB Cash 0.3 0.71/01/2012 FundB MBS 0.7 0.32/01/2012 FundA Cash 0.1 0.52/01/2012 FundA MBS 0.9 0.52/01/2012 FundB Cash 0.2 0.72/01/2012 FundB MBS 0.8 0.33/01/2012 FundA Cash 0.7 0.63/01/2012 FundA MBS 0.3 0.43/01/2012 FundB Cash 0.5 0.13/01/2012 FundB MBS 0.5 0.9You could do this via simple LEFT JOIN I believe, however there is a little twist with the Date field. You see, in Table 2 I am missing the 02/01/2012, however I would like the Target_Weights on 01/01/2012 to fill that void up until the Date where new target weights have been established.Originally I thought this approach would work:Select *,(Select Top 1 a.Target_Weightfrom Table2 awhere a.fund=b.fund and a.security=b.securityand a.date <= b.dateorder a.date asc) as TARGETfrom Table1 bAny Ideas? Bear in mind, I am trying to make the code as efficient as possible |
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2012-12-11 : 09:07:56
|
[code]Declare @Table1 table(Date Datetime,Fund Varchar(10),Security Varchar(10),Actual_Weight float)Insert into @Table1Select '1/01/2012','FundA' ,'Cash',0.2 union allSelect '1/01/2012' ,'FundA' ,'MBS' ,0.8 union allSelect '1/01/2012' ,'FundB' ,'Cash' ,0.3 union allSelect '1/01/2012' ,'FundB' ,'MBS' ,0.7 union allSelect '2/01/2012' ,'FundA' ,'Cash' ,0.1 union allSelect '2/01/2012' ,'FundA' ,'MBS' ,0.9 union allSelect '2/01/2012' ,'FundB' ,'Cash' ,0.2 union allSelect '2/01/2012' ,'FundB' ,'MBS' ,0.8 union allSelect '3/01/2012' ,'FundA' ,'Cash' ,0.7 union allSelect '3/01/2012' ,'FundA' ,'MBS' ,0.3 union allSelect '3/01/2012' ,'FundB' ,'Cash' ,0.5 union allSelect '3/01/2012' ,'FundB' ,'MBS' ,0.5Declare @Table2 table(Date Datetime,Fund Varchar(10),Security Varchar(10),Target_Weight float)Insert into @Table2Select '1/01/2012','FundA' ,'Cash',0.5 union allSelect '1/01/2012' ,'FundA' ,'MBS' ,0.5 union allSelect '1/01/2012' ,'FundB' ,'Cash' ,0.7 union allSelect '1/01/2012' ,'FundB' ,'MBS' ,0.3 union allSelect '3/01/2012' ,'FundA' ,'Cash' ,0.6 union allSelect '3/01/2012' ,'FundA' ,'MBS' ,0.4 union allSelect '3/01/2012' ,'FundB' ,'Cash' ,0.1 union allSelect '3/01/2012' ,'FundB' ,'MBS' ,0.9Select T1.Date,T1.Fund,T1.Security,T1.Actual_Weight,T2.Target_Weightfrom @Table1 T1OUTER APPLY( Select Top 1 a.Target_Weightfrom @Table2 awhere a.fund = T1.fund and a.security = T1.securityand a.date <= T1.dateorder by a.date desc )T2Date Fund Security Actual_Weight Target_Weight2012-01-01 00:00:00.000 FundA Cash 0.2 0.52012-01-01 00:00:00.000 FundA MBS 0.8 0.52012-01-01 00:00:00.000 FundB Cash 0.3 0.72012-01-01 00:00:00.000 FundB MBS 0.7 0.32012-02-01 00:00:00.000 FundA Cash 0.1 0.52012-02-01 00:00:00.000 FundA MBS 0.9 0.52012-02-01 00:00:00.000 FundB Cash 0.2 0.72012-02-01 00:00:00.000 FundB MBS 0.8 0.32012-03-01 00:00:00.000 FundA Cash 0.7 0.62012-03-01 00:00:00.000 FundA MBS 0.3 0.42012-03-01 00:00:00.000 FundB Cash 0.5 0.12012-03-01 00:00:00.000 FundB MBS 0.5 0.9[/code] |
|
|
Kapital123
Starting Member
31 Posts |
Posted - 2012-12-11 : 16:23:44
|
Thankyou Sir I will try this. |
|
|
|
|
|
|
|