0

I have a 30 GB SQLite 3 database with a table containing 25 million rows. I want to group the table by a string column and create a new table for every group of records, named after the string and containing all columns.

Is this possible with Python and SQLite 3? I could process chunks in Python, but is there a SQL command?

Start table

all_entries

a   b   c   d
hi  4   234 lala
bye 7   342 lulu
bye 5   433 abc
bye 4   234 abd
hi  56  435 abc
hi  3   455 a

Result Tables

bye

a   b   c   d
bye 7   342 lulu
bye 5   433 abc
bye 4   234 abd

hi

a   b   c   d
hi  4   234 lala
hi  56  435 abc
hi  3   455 a
  • I'm not sure if I understand your question correctly. When you write that you want to "...group the table by a string column..." do you mean you actually want to split the data of one table (all_entries) into two separate tables (bye and hi)? – John aka hot2use Oct 4 '18 at 11:21
  • yes exactly, I want to split the data of one table into separate tables – Benni Oct 4 '18 at 11:26
  • Do you require a different solution? Could I add anything to my current answer to raise the quality? – John aka hot2use Oct 9 '18 at 13:18
  • I chose a python sulution with simple .tab tables instead of sqlite, since it was faster – Benni Oct 9 '18 at 16:29
0

This can be achieved as follows according to the official SQLite documentation.

For the bye table you could use the following:

CREATE TABLE bye AS 
SELECT a, 
       b,
       c,
       d,
FROM all_entries
WHERE a = 'bye'

...and for the hi table:

CREATE TABLE bye AS 
SELECT a, 
       b,
       c,
       d,
FROM all_entries
WHERE a = 'hi'

Reference: CREATE TABLE (SQL As Understood By SQLite)

If this is just a temporary solution for something you wish to achieve then add the TEMPORARY keyword:

CREATE TEMPORARY TABLE bye AS...

There are some limitations with this approach which are described in the referenced article.

A table created using CREATE TABLE AS has no PRIMARY KEY and no constraints of any kind. The default value of each column is NULL. The default collation sequence for each column of the new table is BINARY.

Tables created using CREATE TABLE AS are initially populated with the rows of data returned by the SELECT statement. Rows are assigned contiguously ascending rowid values, starting with 1, in the order that they are returned by the SELECT statement.

And you would possibly have to post a question on Stack Overflow for a Python solution.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.