2010-10-26 51 views
1
select datepart(month,s1.Timeperiod) as monthofaum, 
      datepart(YEAR,s1.Timeperiod) as Yearofaum, 
      ISNULL(s2.endingAum,0) as Starting_Aum, 
      s1.endingAum as Ending_Aum 
    from #temp_1 s1 
    left outer join (select * from #temp_1)s2 
    on month(s1.Timeperiod) = dateadd(D,1,month(s2.Timeperiod)) 

此工作完美適用於每月基礎,但假如我需要更改查詢以獲取基於年份的結果 - 我應該在哪裏進行更改?對基於年份的查詢所做的更改

Example 
    monthofaum Yearofaum Starting_Aum   Ending_Aum 
    ----------- ----------- --------------------- --------------------- 
    11   2009  0.00     0.00 
    12   2009  0.00     1059594254.86 
    1   2010  0.00     1083195051.98 
    2   2010  1083195051.98   1125314638.64 
    3   2010  1125314638.64   1212355911.70 
    4   2010  1212355911.70   1270374634.62 
    5   2010  1270374634.62   1265193377.27 
    6   2010  1265193377.27   1260776179.02 
    7   2010  1260776179.02   2599205697.44 
    8   2010  2599205697.44   1323838670.57 

如果你看一下數據可以看出,2010年的前一個月的結束奧姆真理教價值將等於下個月開始的奧姆真理教,但是當涉及到2009年的dec結束奧姆真理教沒有分配給2010年1月開始奧姆。

這是我需要修復的錯誤。

回答

0

這是假設你不關心我認爲這將工作時間......

select datepart(month,s1.Timeperiod) as monthofaum, 
    datepart(YEAR,s1.Timeperiod) as Yearofaum, 
    ISNULL(s2.endingAum,0) as Starting_Aum, 
    s1.endingAum as Ending_Aum 
from #temp_1 s1 
left outer join (select * from #temp_1) s2 
on s1.TimePeriod = DateAdd(year,1,s2.TimePeriod) 



編輯:
或者如果你約時間照顧你能試試這個(我有一個方便的功能)...

1.創建日期功能

CREATE FUNCTION [dbo].[fn_DateOnly](@DateTime DATETIME) 
-- Returns @DateTime at midnight; i.e., it removes the time portion of a DateTime value. 
RETURNS DATETIME 
AS 
BEGIN 
RETURN DATEADD(dd,0, DATEDIFF(dd,0,@DateTime)) 
END 

2.這使您可以:

select datepart(month,s1.Timeperiod) as monthofaum, 
    datepart(YEAR,s1.Timeperiod) as Yearofaum, 
    ISNULL(s2.endingAum,0) as Starting_Aum, 
    s1.endingAum as Ending_Aum 
from #temp_1 s1 
left outer join (select * from #temp_1) s2 
on dbo.fn_DateOnly(s1.TimePeriod) = DateAdd(year,1,dbo.fn_DateOnly(s2.TimePeriod)) 
+0

如果TimePeriod僅包含月份第一天午夜的日期 – Andomar 2010-10-26 22:38:52

+0

@Adomar - 是的,那是真的,但我試圖限定它加入「這是假設你不關心時間」。但我更新了我的答案以解釋時間。 – AGoodDisplayName 2010-10-26 22:45:18

+0

@AGoodDisplayName:這將用於比較幾天,但問題是關於比較幾個月。所以你需要一個'fn_MonthOnly' :) – Andomar 2010-10-26 22:49:14

0

要調整它的一年,剛剛月份和年份進行比較。例如,

select convert(varchar(7), getdate(), 120) 

打印2010-10。應用到您的查詢,你可以重寫on像:

on convert(varchar(7), s1.TimePeriod, 120) = 
    convert(varchar(7), DateAdd(year, 1, s2.TimePeriod), 120) 

P.S.子查詢不是必需的。這條線:

left outer join (select * from #temp_1)s2 

是完全一樣的:

left outer join #temp_1 s2 
+0

(select * from#temp_1)的子查詢也會引起我的錯誤。 – AGoodDisplayName 2010-10-26 22:46:19

0
select 
    datepart(month,s1.Timeperiod) as monthofaum, 
    datepart(YEAR,s1.Timeperiod) as Yearofaum, 
    ISNULL(s2.endingAum,0) as Starting_Aum, 
    s1.endingAum as Ending_Aum 
from 
    #temp_1 s1 
left outer join 
    (select * from #temp_1) s2 on (month(s1.Timeperiod)-1 = month(s2.Timeperiod) 
           or (month(s1.Timeperiod) = 1 and month(s2.Timeperiod) = 12)) 

上面的查詢工作,如果表格中只包含數據爲2年。如果我找出上述查詢的問題,我會通知您