【问题标题】:Date a year from now and check what is the next Term from that Date从现在开始一年后的日期,并检查该日期的下一个学期是什么
【发布时间】:2013-09-04 17:13:21
【问题描述】:

我有这张表,其中包含 TermTermStartDateTermEndDate。我必须考虑 今天的日期,然后检查属于哪个期限(当前期限),然后考虑距当前期限的 TermEndDate 正好 360 天的日期(Calculated_Date)。

一旦我得到那个日期,我必须检查哪个期限在那个日期之后。基本上什么是计算日期之后的 TermStartdate。

注意:

基本上,我需要当前学期和当前学期前一年之间的学生的所有记录。比如说,如果这个词是 Fall 2013 ,我需要从 Spring 2013 开始的记录。不应该考虑 Fall 2012。

编辑:

样本表

  Term           TermStartDate    TermEndDate
 Fall 2012          2012/08/27     2012/12/15
 Spring 2013        2013/01/14     2013/04/26
 Sumr I 2013        2013/05/06     2013/06/29
 Sumr II 2013       2013/07/01     2013/08/24
 Fall 2013          2013/08/26     2013/12/14 
 Spring 2014        2014/01/13     2014/04/26      

第 1 步:GetDate()

第 2 步:检查 GetDate() 之后的 TermEndDate(给出当前期限)

第 3 步:计算当前学期结束日期前 360 天的日期

第 4 步:在第 3 步中计算的日期之后的第一个期限

【问题讨论】:

  • 请提供样本数据和所需结果,而不是文字问题
  • 所以澄清一下,您希望第一个术语的开始日期在您提供的日期参数之后?

标签: sql sql-server-2008 tsql


【解决方案1】:
DECLARE @lastTermEnd

SELECT @lastTermEnd=DATEADD(d,-360,TermEndDate)
FROM Students
where GETDATE() between TermStartDate and TermEndDate

SELECT TOP 1 *
from Students
WHERE TermStartDate between @lastTermEnd and GETDATE()
ORDER BY TermStartDate

这将列出计算日期之后的第一个术语。

更新:

DECLARE @lastTermEnd datetime
DECLARE @TermEnd datetime

SELECT @TermEnd=TermEndDate
FROM Students
where GETDATE() between TermStartDate and TermEndDate

SET @lastTermEnd=DATEADD(d,-360,@TermEnd)

SELECT TOP 1 TermStartDate,@TermEnd
from Students
WHERE TermStartDate between @lastTermEnd and GETDATE()
ORDER BY TermStartDate

【讨论】:

  • dcp1986:你能告诉我我们如何获得 >=GetDate() 的第一个 termEndDate。在查询本身内。
【解决方案2】:

我认为你把问题复杂化了,但按照你的要求,试试这个:

DECLARE @terms TABLE(term varchar(50),termStartDate date, termEndDate date)
INSERT INTO @terms VALUES('Fall 2012','8/27/2012','12/15/2012')
INSERT INTO @terms VALUES('Spring 2013','1/14/2013','4/26/2013')
INSERT INTO @terms VALUES('Sumr I 2013','5/6/2013','6/29/2013')
INSERT INTO @terms VALUES('Sumr II 2013','7/1/2013','8/24/2013')
INSERT INTO @terms VALUES('Fall 2013','8/26/2013','12/14/2013')
INSERT INTO @terms VALUES('Spring 2014','1/13/2014','4/26/2014')

DECLARE @today date =GETDATE()
SELECT @today = termEndDate 
    FROM @terms 
    WHERE termStartDate<=@today AND termEndDate>=@today
SELECT term 
    FROM @terms 
    WHERE termStartDate>=DATEADD(d,-360,@today) AND termStartDate<=GETDATE()

这将列出当前期限结束前 360 天期间包含的所有期限。

更新

SELECT min(termStartDate)startDate FROM (
    SELECT termStartDate 
        FROM @terms 
        GROUP BY termStartDate 
        HAVING termStartDate>=DATEADD(d,-360,@today) 
               AND termStartDate<=GETDATE()
)z

将获得最早任期的开始日期。

【讨论】:

  • Davids,上面的解决方案几乎可以工作,但是 "WHERE termStartDate>=DATEADD(d,-360,@today) AND termStartDate
【解决方案3】:

所以我认为这样的事情就是你要问的,但我实际上并不完全确定。你的问题很难理解。

SELECT *
FROM myStudents
WHERE theDate BETWEEN DATEADD(yy,-1,currentTerm) AND currentTerm

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-11
    • 2016-08-28
    • 2015-05-04
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    相关资源
    最近更新 更多