Crystal Reports需要按派生日期范围分组

时间:2012-10-23 19:27:30

标签: sql plsql group-by date-range crystal-reports-2010

长时间列表,第一次来电。我正在使用Crystal Reports 2010。

如果音量没有变化,我有每日交易信息需要组合在一起。这是数据的样子。

Trade#BegDate EndDate Volume

1        1/1/2012    1/2/2012    500
1        1/2/2012    1/3/2012    500
1        1/3/2012    1/4/2012    1000
1        1/4/2012    1/5/2012    750
1        1/5/2012    1/6/2012    750
1        1/6/2012    1/7/2012    500
1        1/7/2012    1/8/2012    500
1        1/8/2012    1/9/2012    500

我需要它看起来像这样。

Trade#DateRange Volume

1        1/1/2012 - 1/3/2012  500
1        1/3/2012 - 1/4/2012  1000
1        1/4/2012 - 1/6/2012  750
1        1/6/2012 - 1/9/2012  500

我需要按派生日期范围进行分组,但我不确定如何使用Crystal完成此操作。任何想法??

4 个答案:

答案 0 :(得分:4)

with w as (
 select 1 id, to_date('1/1/2012', 'mm/dd/yyyy') start_date, to_date('1/2/2012', 'mm/dd/yyyy') end_date, 500 sales_volume 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'), 500 from dual      
 )

,t as (select sales_volume
             ,start_date
             ,end_date 
             ,lag (sales_volume,1) over (order by start_date) prev_sales_volume
       from w
       order by start_date)
,u as (select *
       from t 
       where nvl(prev_sales_volume,-1) != sales_volume
       order by start_date)
select start_date
      ,nvl(lead (start_date,1) over (order by start_date),(select max(end_date) from w))   end_date 
      ,sales_volume
from  u
order by start_date

答案 1 :(得分:1)

我使用“X-2”抑制功能来隐藏除每行之外的所有行,而使用Previous()和Next()来查找端点。

BegDate集团;抑制细节和组页脚部分。

在组标题

中的某处创建一个{@UpdateCurrentBegDate}函数
WhilePrintingRecords;    
Global DateVar CurrentBegDate;

If PreviousIsNull({table.Volume}) or Previous({table.Volume}) <> {table.Volume}
    Then ( //We found a new range
        CurrentBegDate = {table.BegDate};
     );

""; //Display nothing on screen

还有一个{@DisplayBegDate}函数,您当前正在显示BegDate

EvaluateAfter({@UpdateCurrentBegDate});
Global DateVar CurrentBegDate;

转到Section Expert并单击Suppress选项旁边的“X-2”(对于组标题部分)。这是那里的公式

Not(NextIsNull({table.Volume}) or Next({table.Volume}) <> {table.Volume})

如果您需要进行总计或其他计算,您可以在{@UpdateCurrentBegDate}函数中执行此操作(并且您希望将名称更改为{@UpdateCurrentValues}或类似的名称)。如果您只想在组更改时更改内容,您还可以创建一个检查Next()信息的新函数 - 使用默认的总计函数将包含隐藏值。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

这是我能提出的最优雅的解决方案

WITH DirectTrades(tradeid, SourceDate, EndDate, Volume, Row, KillRow, Depth) AS 
(
    SELECT  tradeid
            BegDate AS SourceDate, 
            EndDate,
            Volume,
            ROW_NUMBER() over (partition by Table# order by BegDate) AS Row,
            ROW_NUMBER() over (partition by Table# order by BegDate) AS KillRow,
            0 AS Depth
    FROM    Trade
    UNION ALL
    SELECT  t1.Tradeid
            dt.SourceDate, 
            t1.EndDate, 
            t1.Volume, 
            dt.Row, 
            dt.Row + dt.Depth + 1,
            dt.Depth + 1
    FROM    Trade AS t1
            INNER JOIN 
            DirectTrades AS dt ON 
                    t1.BegDate=dt.EndDate AND 
                    t1.Volume=dt.Volume AND
                    t1.tradeid=dt.Tradeid
)

SELECT  dt1.Tradeid
        dt1.SourceDate,
        dt1.EndDate,
        dt1.Volume 
FROM    DirectTrades dt1
        INNER JOIN
        (
        SELECT dt2.Row, 
                MAX(dt2.KillRow) AS KillRow
        FROM    DirectTrades dt2
        WHERE   dt2.Row NOT IN
                (
                SELECT dt3.KillRow
                FROM    DirectTrades dt3
                WHERE   dt3.Depth <> 0  
                )
        GROUP BY    dt2.Row
        ) dt4 ON dt1.Row=dt4.Row AND dt1.KillRow=dt4.KillRow 
ORDER BY SourceDate