SQL Server-选择即将到来的30天的数据,其中1年



对于我的项目,我需要找到一个查询,以查找即将到来的30天的数据,其中1年。

这是一个示例:

- 我接受了2014-01-31的培训,要完成我的培训,我需要在1年(2015-01-31)进行提醒。

- 但是,我想在我的数据库中选择此提醒,在提醒日期之前至少30天。

- 因此,我的目标是提醒2015-01-31直到2015-01-31。

这是我的代码的一个示例,因为我的英语不好(我是法语IT学生),我无法解释很好...:/

select nom, libelle_produit, nom_produit, date_debut_formation, date_fin_formation,
       DATEADD (day , 365 , date_fin_formation) as 'Date limite' 
from T_PRODUIT 
    join T_FORMATION  on T_FORMATION.id_produit=T_PRODUIT.id_produit 
    join T_SUIT on T_SUIT.id_formation=T_FORMATION.id_formation 
    join T_EMPLOYES on T_EMPLOYES.id_employe = T_SUIT.id_employe 
where DATEADD(year,1,year(date_fin_formation)) <= DATEADD(year,1,getdate())
  and DATEADD(month,1,month(date_fin_formation)) <= DATEADD(month,1,getdate())
  and DATEADD (day,1,day(date_fin_formation)) <= DATEADD(day,-30,GETDATE())

你能帮我吗?这不会给我一个很好的结果...

如果我正确理解您的问题,您想要

如果

,所有提醒都将显示
  • date_fin_formation小于(今天的日期-1岁) 30天
  • date_fin_formation大于今天的日期-1岁

即。通知应从(今天的日期-1岁) 30天到结束(今天的日期-1年)

您可以使用诸如 DATEADD(year,1,date_fin_formation) BETWEEN DATEADD(day,-30,getdate()) AND GETDATE()之类的Where子句,您的查询将是。

select nom, libelle_produit, nom_produit, date_debut_formation, date_fin_formation, DATEADD (day , 365 , date_fin_formation) as 'Date limite' 
  from T_PRODUIT 
    join T_FORMATION  on T_FORMATION.id_produit=T_PRODUIT.id_produit 
    join T_SUIT on T_SUIT.id_formation=T_FORMATION.id_formation 
    join T_EMPLOYES on T_EMPLOYES.id_employe = T_SUIT.id_employe 
     WHERE DATEADD(year,1,date_fin_formation) BETWEEN  DATEADD(day,-30,getdate()) AND GETDATE()

相关内容

最新更新