【问题标题】:Select last 3 month Record in Month January based on User Table根据用户表选择一月份的最近 3 个月记录
【发布时间】:2015-01-06 01:57:56
【问题描述】:

我在数据库中有如下记录

ID        RDate       Name
 1         1/1/2015     A
 2         1/1/2015     B
 3         31/12/2014   A
 4         3/12/2014    A
 5         22/10/2014   C
 6         1/8/2014     D

我希望根据当前日期选择当前月份记录和最近 3 个月记录。

我有如下查询

Select * from tbl1 where month(RDate) = Month(getdate()) and Year(RDate) = Year(getdate())

这我可以得到当前月份的记录。我可以在查询下面得到上个月的记录使用情况

Select * from tbl1 where month(RDate) = Month(Dataadd(month, -1, RDate)) and  Year(RefDate) = Year(DateAdd(year, -1, RDate))

问题是当当前月份是 2015 年 4 月时,我最近 3 个月的记录将是 2015 年 3 月、2015 年 2 月和 2015 年 1 月。对于年份的查询将不正确,因为查询中的年份为负 1。如何使查询动态化?

【问题讨论】:

    标签: sql sql-server sql-server-2008


    【解决方案1】:

    您可以使用dateadddatediff 函数

    dateadd(month, datediff(month, 0, getdate())-3, 0) 给出当前月份的开始 - 3 个月

    如果今天运行,上面将给出 2014 年 10 月 1 日。

    dateadd(month, datediff(month, 0, getdate()), -1) 给出上个月的最后一天

    如果今天运行,上面将给出 2014 年 12 月 31 日。

    select * from Table1
    where RDate between
    dateadd(month, datediff(month, 0, getdate())-3, 0),
    AND 
    dateadd(month, datediff(month, 0, getdate()), -1)
    

    【讨论】:

      【解决方案2】:

      这个呢:

      CREATE TABLE tbl1(
          ID      INT,
          RDate   DATE,
          Name    VARCHAR(5)
      )
      INSERT INTO tbl1 VALUES
      (1, '20150101', 'A'),
      (2, '20150101', 'B'),
      (3, '20141231', 'A'),
      (4, '20141203', 'A'),
      (5, '20141022', 'C'),
      (6, '20140801', 'D');
      
      SELECT * 
      FROM tbl1 
      WHERE
          (
              RDate >= DATEADD(MM, DATEDIFF(MM, 0, GETDATE()), 0) -- First day of the current month
              AND RDate < DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) + 1, 0) -- First day of the next month
          )
          OR (
              RDate >= DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) - 3, 0) -- 3 months less than the current month
              AND RDate < DATEADD(MM, DATEDIFF(MM, 0, GETDATE()), 0)  -- First day of the current month
          )
      DROP TABLE tbl1
      

      当然你可以结合WHERE子句:

      WHERE
          RDate >= DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) - 3, 0) -- 3 months less than the current month
          AND RDate < DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) + 1, 0) -- First day of the next month
      

      有关更常见的日期例程,请参阅:http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/

      【讨论】:

        【解决方案3】:

        比较第一个和第二个查询的结果。如果您想要本月和过去 3 个完整日历月的数据,请使用第二个查询。

        
        declare @t table (ID int, RDate date, Name char)
        
        

        insert into @t values (1,'2015-01-01','A'), (2,'2015-01-01','B'), (3,'2014-12-31','A'), (4,'2014-12-03','A'), (5,'2014-10-22','C'), (6,'2014-09-01','D'), (7,'2014-09-06','D'), (8,'2014-09-05','D'), (9,'2014-08-01','D')

        declare @comparison_date date = '2015-01-06'

        select * from @t where RDate > dateadd(month,-4,@comparison_date)

        select * from @t where RDate > dateadd(month,-4,dateadd(day,-datepart(dd,@comparison_date),@comparison_date))

        【讨论】:

          猜你喜欢
          • 2014-11-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-28
          • 2018-06-28
          相关资源
          最近更新 更多