Ahhhh this t-sql stuff is frustrating. The code below is what I have but it isn't returning the correct data.Summary: 4 tables (tOrders, tCalendar, tPrice, tDividends)I'm trying to get all orders from a tickerid (tId) and then add to the set of records all dividends paid by tId, rId, hId, and also add to the set of records all rebalance dates according to @rebalI then want to join the prices of tId,rId,hId from tPrice onto the table of records above.The code that I have is returning a duplicate record for each record and is not including the records from the UNION ALL tCalendar query.Any help is greatly appreciatedSample Data (reduced to include less columns than original query) tCalendar dt tdEOM 2/22/2007 0 2/23/2007 0 2/24/2007 0 2/25/2007 0 2/26/2007 0 2/27/2007 0 2/28/2007 1 3/1/2007 0 3/2/2007 0 tOrders Date TickerId TranId Qty2/23/2007 5 1 1002/25/2007 4 2 2003/5/2007 4 1 500 tDividends ExDate TickerId TranId Amt2/24/2007 5 5 0.152/28/2007 4 5 0.253/2/2007 73 5 0.15 tPrice Date TickerId Price_Close 2/22/2007 15 23.50 2/23/2007 15 23.25 2/24/2007 15 23.75 2/25/2007 15 23.60 2/26/2007 15 23.35 2/27/2007 15 23.85 2/28/2007 15 23.70 3/1/2007 15 23.45 3/2/2007 15 23.95 3/3/2007 15 23.80 3/4/2007 15 23.55 3/5/2007 15 24.05 2/22/2007 86 17.63 2/23/2007 86 17.44 2/24/2007 86 17.81 2/25/2007 86 17.70 2/26/2007 86 17.51 2/27/2007 86 17.89 2/28/2007 86 17.78 3/1/2007 86 17.59 3/2/2007 86 17.96 3/3/2007 86 17.85 3/4/2007 86 17.66 3/5/2007 86 18.04 2/22/2007 73 52.88 2/23/2007 73 52.31 2/24/2007 73 53.44 2/25/2007 73 53.10 2/26/2007 73 52.54 2/27/2007 73 53.66 2/28/2007 73 53.33 3/1/2007 73 52.76 3/2/2007 73 53.89 3/3/2007 73 53.55 3/4/2007 73 52.99 3/5/2007 73 54.11
Result Set from Query should beDt TickerId TranId Qty tPrice hPrice rPrice2/23/2007 5 1 100 23.25 52.31 17.44 2/25/2007 4 2 200 23.60 53.10 17.70 3/5/2007 4 1 500 24.05 54.11 18.04 2/24/2007 5 5 0.15 23.75 53.44 17.81 2/28/2007 4 5 0.25 23.70 53.33 17.78 3/2/2007 73 5 0.15 23.95 53.89 17.96 2/28/2007 0 6 0 23.70 53.33 17.78
DECLARE @tId smallint DECLARE @hId smallint DECLARE @rId smallint DECLARE @rebal tinyint DECLARE @stdt smalldatetimeDECLARE @endt smalldatetimeSET @tId = 15SET @hId = 73SET @rId = 86SET @rebal = 4SET @stdt = (SELECT MIN([DATE]) FROM dbo.tOrders WHERE tickerid = @tid)SET @endt = (SELECT MAX([DATE]) FROM dbo.tOrders WHERE tickerid = @tid) SELECT a.DATE,a.TickerId,a.TranId,a.Qty,a.Prin,a.Com,a.Amt, b.Price_Close as HedgePrice, c.Price_Close AS ReinvestPrice, d.Price_Close AS TickPrice FROM ( SELECT DATE,TickerId,TranId,Qty,Prin,Com,Amt FROM dbo.tOrders WHERE tickerid = @tId UNION ALL SELECT ExDate as Date, TickerId, 5 as TranId,0 as Qty,Amount as Prin, 0 as Com, 0 as Amt FROM dbo.tblDividends UNION ALL SELECT DT, 0 as TickerId, 6 as TranId, 0 as Qty, 0 as Prin, 0 as Com, 0 as Amt FROM dbo.tCalendar WHERE ( (@rebal=0 AND TD = 1) OR (@rebal=1 AND TDEOY=1) OR (@rebal=4 AND TDEOQ=1) OR (@rebal=12 AND TDEOM=1) OR (@rebal=52 AND TDEOW=1) ) ) a INNER JOIN dbo.tPrice b ON a.Date = b.Date INNER JOIN dbo.tPrice c ON a.Date = c.Date INNER JOIN dbo.tPrice d ON a.Date = d.DATE WHERE (a.TickerId = @tId) AND (b.TickerId = @hId) AND (c.TickerId = @rId) AND (d.TickerId=@tId) AND (a.Date>@stdt) AND (a.Date <= @endt) OR (a.TickerId = @hId) AND (b.TickerId = @hId) AND (c.TickerId = @rId) AND (d.TickerId=@tId) AND (a.Date>@stdt) AND (a.Date <= @endt) OR (a.TickerId = @rId) AND (b.TickerId = @hId) AND (c.TickerId = @rId) AND (d.TickerId=@tId) AND (a.Date>@stdt) AND (a.Date <= @endt) ORDER By a.Date asc, a.qty asc