What is the best way to create and populate a numbers table?

后端 未结 11 2506
悲&欢浪女
悲&欢浪女 2020-11-21 13:28

I\'ve seen many different ways to create and populate a numbers table. However, what is the best way to create and populate one? With \"best\" being defined from most to l

11条回答
  •  悲&欢浪女
    2020-11-21 14:03

    I use numbers tables for primarily dummying up reports in BIRT without having to fiddle around with dynamic creation of recordsets.

    I do the same with dates, having a table spanning from 10 years in the past to 10 years in the future (and hours of the day for more detailed reporting). It's a neat trick to be able to get values for all dates even if your 'real' data tables don't have data for them.

    I have a script which I use to create these, something like (this is from memory):

    drop table numbers; commit;
    create table numbers (n integer primary key); commit;
    insert into numbers values (0); commit;
    insert into numbers select n+1 from numbers; commit;
    insert into numbers select n+2 from numbers; commit;
    insert into numbers select n+4 from numbers; commit;
    insert into numbers select n+8 from numbers; commit;
    insert into numbers select n+16 from numbers; commit;
    insert into numbers select n+32 from numbers; commit;
    insert into numbers select n+64 from numbers; commit;
    

    The number of rows doubles with each line so it doesn't take a lot to produce truly huge tables.

    I'm not sure I agree with you that it's important to be created fast since you only create it once. The cost of that is amortized over all the accesses to it, rendering that time fairly insignificant.

提交回复
热议问题