简体   繁体   中英

Add a certain amount of days to a date if the date meets a condition

I've been tasked with pulling durations between two dates. I'm doing the averages, etc in Excel, so I am only pulling the start and end dates. Since we have one data source that does not have the end dates added, I am pulling the oldest row (there are multiple transactions) where the status = 'closed'. The only issue is that some of these dates were actually prior to the 'reported date'. So A bit of logic was added to use the accounting period where the status is set to closed if the end or 'closed' date is greater than the reported date. This is also using today's date as the end date if the status is still set to 'open'. My issue is that the accounting date is the first of the month. What I need to do is change that accounting date for the column 'end_date' to either the last day of the month, or the 28th of the month. I searched on how to do this quite a bit and found some help, but I can't figure out how to get what I need when these other stipulations are in play. Any help would be appreciated.

Summary: I need to add 27 days to the dates in the 'end_date' column IF they are using the accounting date and not today's date (these will only be for records that come from the 'xls' data source.

Here is the query I am running:

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      

The DATEADD() function can be used to add or subtract a unit of time to/from a date.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM