Crystal Reports Need to Group by Derived Date Range

后端 未结 4 1728
迷失自我
迷失自我 2020-12-18 06:58

Long time listner, first time caller. I\'m using Crystal Reports 2010.

I have daily trade information that I need to group together if the volume doesn\'t change. H

4条回答
  •  星月不相逢
    2020-12-18 07:04

    with w as (
         select 1 id, to_date('1/1/2012', 'mm/dd/yyyy') db, to_date('1/2/2012', 'mm/dd/yyyy') de, 500 s from dual
         union all
         select 1, to_date('1/2/2012', 'mm/dd/yyyy'), to_date('1/3/2012', 'mm/dd/yyyy'), 500 from dual
         union all
         select 1, to_date('1/3/2012', 'mm/dd/yyyy'), to_date('1/4/2012', 'mm/dd/yyyy'), 1000 from dual
         union all
         select 1, to_date('1/4/2012', 'mm/dd/yyyy'), to_date('1/5/2012', 'mm/dd/yyyy'), 750 from dual
         union all
         select 1, to_date('1/5/2012', 'mm/dd/yyyy'), to_date('1/6/2012', 'mm/dd/yyyy'), 750 from dual
         union all
         select 1, to_date('1/6/2012', 'mm/dd/yyyy'), to_date('1/7/2012', 'mm/dd/yyyy'), 500 from dual
         union all
         select 1, to_date('1/7/2012', 'mm/dd/yyyy'), to_date('1/8/2012', 'mm/dd/yyyy'), 500 from dual
         union all
         select 1, to_date('1/8/2012', 'mm/dd/yyyy'), to_date('1/9/2012', 'mm/dd/yyyy'), 510 from dual      
         )
    
    select tmin.db, tmax.de, tmin.s
    from
    (     
      select 
           row_number() over (order by db) id,
               db,
               s
      from   
      ( 
          select
               db,
               s,
               case 
                      when ps is null
                           then 1
                      when ps != s
                           then row_number() over (order by db) 
                   else 0 end num
           from (
    
               select 
                       (db)
                      , (de)
                      , lag (s,1) over (ORDER BY db) ps                
                      , s
    
    
               from w
               ) t
      ) t1
      where num != 0
     ) tmin,
    
     (select 
         row_number() over (order by db) id,
             de,
             s
    from   
    (      
        select
               db,
                 de,
    
                 s,
                 case 
                    when ps is null
                         then 1
                    when ps != s
                         then row_number() over (order by de desc) 
                 else 0 end num
         from (
    
             select 
                      db
                    ,(de)
                    , lag (s,1) over (ORDER BY de desc) ps                
                    , s                
    
             from w
             order by db
             ) t
     ) t1
    where num != 0) tmax
    
    where tmin.id = tmax.id
    

提交回复
热议问题