基于年份的查询更改

时间:2010-10-26 22:23:13

标签: sql sql-server sql-server-2005 tsql

    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年上个月的结束Aum值将等于下个月的Start Aum,但到2009年时,结束Aum未分配到2010年1月开始Aum

这是我需要修复的错误。

3 个答案:

答案 0 :(得分: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.创建dateonly函数

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))

答案 1 :(得分: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

答案 2 :(得分: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年的数据,则上述查询有效。如果我找出上述查询的问题

,我会告诉大家