Tally Table

Moderator: NorbertKrupa

Post Reply
doug_harmon
Beginner
Beginner
Posts: 36
Joined: Fri Feb 17, 2012 6:09 pm
Contact:

Tally Table

Post by doug_harmon » Wed Mar 28, 2012 4:29 pm

I needed to create a tally table. Here's how I did it.

Code: Select all

#!/bin/bash
# Create a Tally Table.

export SQLFile=/tmp/TallyLoad.sql
echo -n "" > $SQLFile
export DataFile=/tmp/Tally.dat
echo -n "" > $DataFile

export i
for i in {1..100000}
do
    echo  "${i}"   >> $DataFile
done

echo 'DROP TABLE Tally;' >> $SQLFile
echo 'CREATE TABLE Tally(N INT) UNSEGMENTED ALL NODES;' >> $SQLFile
echo 'COPY Tally FROM ' "'""${DataFile}""'"  ';' >> $SQLFile

vsql < $SQLFile

rm $SQLFile
rm $DataFile

User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Re: Tally Table

Post by JimKnicely » Wed Mar 28, 2012 9:59 pm

That's cool!

Another way to create that sort of table is with SQL...

Code: Select all

dbadmin=> create table tally as select n from (select row_number() over() n from columns cross join tables) as foo where n <= 100000;
CREATE TABLE
dbadmin=> \d tally;
                                List of Fields by Tables
 Schema | Table | Column | Type | Size | Default | Not Null | Primary Key | Foreign Key
--------+-------+--------+------+------+---------+----------+-------------+-------------
 public | tally | n      | int  |    8 |         | f        | f           |
(1 row)

dbadmin=> select count(*) from tally;
 count
--------
 100000
(1 row)
I think this is doing the same thing you were doing :)
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

doug_harmon
Beginner
Beginner
Posts: 36
Joined: Fri Feb 17, 2012 6:09 pm
Contact:

Re: Tally Table

Post by doug_harmon » Thu Mar 29, 2012 3:23 pm

Thanks for sharing, that's an elegant solution.

Post Reply

Return to “Vertica Database Development”