【问题标题】:Add a certain amount of days to a date if the date meets a condition如果日期满足条件,则为日期添加一定天数
【发布时间】:2014-07-22 19:45:54
【问题描述】:

我的任务是拉动两个日期之间的持续时间。我在 Excel 中做平均值等,所以我只拉开始和结束日期。由于我们有一个未添加结束日期的数据源,因此我将提取状态 = '已关闭' 的最旧行(有多个事务)。唯一的问题是其中一些日期实际上早于“报告日期”。因此,如果结束或“关闭”日期大于报告日期,则添加了一些逻辑来使用状态设置为关闭的会计期间。如果状态仍设置为“打开”,这也使用今天的日期作为结束日期。我的问题是会计日期是每月的第一天。我需要做的是将“end_date”列的会计日期更改为该月的最后一天或该月的 28 日。我搜索了很多如何做到这一点并找到了一些帮助,但是当这些其他规定在起作用时,我无法弄清楚如何获得我需要的东西。任何帮助,将不胜感激。

总结:如果他们使用会计日期而不是今天的日期,我需要在“end_date”列中的日期上添加 27 天(这些仅适用于来自“xls”数据源的记录。

这是我正在运行的查询:

SELECT product_sub.product as product       
      ,product_sub.sub_product as sub_product       
      ,fl_dtl.num_claim as num_claim    
      ,fl_dtl.data_source as data_source    
      ,fl_dtl.status as status  
      ,fl_dtl.d_reported as d_reported  
      ,CASE 
         WHEN fl_dtl.data_source = 'xls' and fl_dtl.d_reported > close_xls.d_closed     THEN fl_dtl.d_reported  
         WHEN fl_dtl.data_source = 'xls' and fl_dtl.d_reported <= close_xls.d_closed THEN close_xls.d_closed
         ELSE fl_dtl.d_closed
       END as close_date    
      ,CASE 
         WHEN fl_dtl.data_source = 'xls' and fl_dtl.d_reported > close_xls.d_closed THEN COALESCE(fl_dtl.d_reported,GETDATE())  
         WHEN fl_dtl.data_source = 'xls' and fl_dtl.d_reported <= close_xls.d_closed THEN COALESCE(close_xls.d_closed,GETDATE())    
     ELSE COALESCE(fl_dtl.d_closed,GETDATE())
   END as end_date  
from        
(select num_claim       
       ,pol_num     
   ,data_source 
       ,max(status) as status       
       ,min(d_reported) d_reported      
       ,min(d_closed) d_closed      
 from COMAPANY.dbo.fact_loss        
 group by num_claim     
         ,pol_num       
         ,data_source) fl_dtl
left outer join         

(select fp.pol_num      
       ,fp.product      
       ,fp.sub_product  
 from COMAPNY.dbo.fact_prem fp      
 inner join     
 (select pol_num        
        ,max(id_prem) id_prem       
  from COMAPNY.dbo.fact_prem        
  where amt_type = 'Premium'        
  group by pol_num) max_fp      
  ON fp.pol_num = max_fp.pol_num        
 AND fp.id_prem = max_fp.id_prem) as product_sub        
 ON fl_dtl.pol_num = product_sub.pol_num        
left outer join     

(select ln.num_claim        
       ,MIN(d_book) as d_closed     
 from COMAPNY.dbo.v_COMPANY_losses_new ln       
 where status like '%clos%'     
   and num_claim is not null        
 group by ln.num_claim) close_xls       
 ON fl_dtl.num_claim = close_xls.num_claim      

【问题讨论】:

  • 你熟悉 DATEADD() 函数吗?
  • 要在 SL-Server 中添加天数,请使用:DATEADD(d, number-of-days to add, Date to add to)
  • @MartinK。我是,但我无法弄清楚语法,因为查询中有这么多规则
  • 你能简明扼要地描述一下这种情况发生时的情况吗?
  • 也许从比 this query 更简单的东西开始,以便首先了解 CASE 表达式的基础知识。

标签: sql sql-server tsql date format


【解决方案1】:

DATEADD() 函数可用于在日期之间添加或减去时间单位。

SELECT DATEADD(unit of time, number to add, base date)
SELECT DATEADD(day,29,GETDATE())

【讨论】:

    猜你喜欢
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    相关资源
    最近更新 更多