在Crystal Report中创建频率间隔

时间:2015-05-04 16:12:03

标签: oracle plsql oracle11g crystal-reports

我正在尝试在水晶报告中创建频率间隔的数据集,如下所示。第一列是rowid,第二列是开始间隔,第三列是结束间隔,第四列是间隔名称。

1   0   29  0 - 29
2   30  59  30 - 59
3   60  89  60 - 89
4   90  119 90 - 119
5   120 149 120 - 149
6   150 179 150 - 179
7   180 209 180 - 209
8   210 239 210 - 239
9   240 269 240 - 269
10  270 299 270 - 299
11  300 329 300 - 329
12  330 359 330 - 359
13  360 389 360 - 389
14  390 419 390 - 419
15  420 449 420 - 449
16  450 479 450 - 479
17  480 509 480 - 509
18  510 539 510 - 539
19  540 569 540 - 569
20  570 599 570 - 599
21  600 629 600 - 629
22  630 659 630 - 659
23  660 689 660 - 689
24  690 719 690 - 719
25  720 749 720 - 749
26  750 779 750 - 779
27  780 809 780 - 809
28  810 839 810 - 839
29  840 869 840 - 869
30  870 899 870 - 899

我可以编写一个CTE来生成这个间隔,这样我就可以直接在水晶报表中使用它而无需在数据库端编写函数吗?以下是我写的代码:

declare intervalStart integer := 0;
        intervalEnd integer := 900;
        intervalMins varchar(10) := 30;
        totalIntervals number := 0;

begin
    begin
        execute immediate 'create global temporary table intervalTable  (row_Id int not null, intStart integer, intEnd integer, intervalName varchar2(25))ON COMMIT DELETE ROWS';
        exception when others then dbms_output.put_line(sqlerrm);
    end;
    totalIntervals := intervalEnd/intervalMins;
    --dbms_output.put_line(totalIntervals);

    for i in 1 ..totalIntervals loop
        intervalStart := 0;
        intervalEnd := 0;
        intervalStart := intervalStart + (i-1)*intervalMins;
        intervalEnd := intervalEnd + (i*intervalMins)-1;
        --dbms_output.put_line(intervalStart || ' - ' || intervalEnd);
        insert into intervalTable 
        (
            row_id,
            intStart,
            intEnd,
            intervalName
        )
        values(i, intervalStart, intervalEnd, (intervalStart || ' - ' || intervalEnd));
    end loop;


end;

1 个答案:

答案 0 :(得分:2)

我想你想要这样的东西:

with freq_data as (
    select level as id, (level-1)*30 as start_interval, ((level-1)*30) + 29 as end_interval, (level-1)*30 || ' - ' || to_char(((level-1)*30) + 29) as label
    from dual
    connect by level <= 30
    order by level
)
select * from freq_data;

输出

ID  START_INTERVAL  END_INTERVAL    LABEL
1   0   29  0 - 29
2   30  59  30 - 59
3   60  89  60 - 89
4   90  119 90 - 119
5   120 149 120 - 149
6   150 179 150 - 179
7   180 209 180 - 209
8   210 239 210 - 239
9   240 269 240 - 269
10  270 299 270 - 299
11  300 329 300 - 329
12  330 359 330 - 359
13  360 389 360 - 389
14  390 419 390 - 419
15  420 449 420 - 449
16  450 479 450 - 479
17  480 509 480 - 509
18  510 539 510 - 539
19  540 569 540 - 569
20  570 599 570 - 599
21  600 629 600 - 629
22  630 659 630 - 659
23  660 689 660 - 689
24  690 719 690 - 719
25  720 749 720 - 749
26  750 779 750 - 779
27  780 809 780 - 809
28  810 839 810 - 839
29  840 869 840 - 869
30  870 899 870 - 899

在连接查询中使用上述示例:

create table my_test 
(
    num number
    -- other important data ...
);

-- insert some random numbers
insert into my_test
select trunc(DBMS_RANDOM.VALUE(0,900))
from dual
connect by level <= 10;
commit;

现在加入以获取每个num字段的标签:

with freq_data as (
    select level as id, (level-1)*30 as start_interval, ((level-1)*30) + 29 as end_interval, (level-1)*30 || ' - ' || to_char(((level-1)*30) + 29) as label
    from dual
    connect by level <= 30
    order by level
)
select t.num, d.label
from my_test t
left join freq_data d ON (t.num between d.start_interval and d.end_interval);

输出:

NUM LABEL
64  60 - 89
73  60 - 89
128 120 - 149
154 150 - 179
267 240 - 269
328 300 - 329
550 540 - 569
586 570 - 599
745 720 - 749
795 780 - 809