Solved

Re-sorting Sqlite table

Posted on 2010-09-22
5
657 Views
Last Modified: 2012-05-10
I would like to know if there is an SQL command that would re-sort the a table in a SQLite database. I have a table with 4 columns, which gets data added and removed from it frequently.
Just from a neatness point of view (as the messiness of the table does not have any negative impact on SQL queries on the table data), I'd like to be able to sort the table.
I've attached a screen grab of the table where you can see 4 records with Dept_Id value 1 and 4 records with Dept_id value 2. I'd like to sort the table so I get all the Value 1s followed by the Value 2s etc. Screen grab of SQlite table I'd like to sort.
0
Comment
Question by:kalbarriman
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:jon47
ID: 33732920
There isn't an SQL command which reorders the records stored in a table.  What you can do is pull the data out sorted in whatever way you choose- so rather than browsing the table, use the "Execute SQL" tab in your sqlite browser and use a query like this:

SELECT * FROM DeptExts ORDER BY `Dept Id`, `Extn Name`

(Those a back-ticks around the column names)

Jon

0
 

Author Comment

by:kalbarriman
ID: 33732958
Thanks Jon. Yes, no problem pulling the data from the table in any sort order I want, but sorry to hear, (although not surprised as I've been checking on the NET and reading books and can't find anything) that I can't actually re-sort the table itself.
I'll leave the question open for a bit longer just in case someone else can magic up a solution but otherwise, thanks for your input.
Andrew
0
 
LVL 2

Accepted Solution

by:
jon47 earned 250 total points
ID: 33733396
Actually, here's some SQL that does what you want... it takes four statements-

CREATE TABLE DeptExtsSorted (Dept_Name TEXT, Extn_Name TEXT, Dept_Id NUMERIC, Dept_Ext NUMERIC);
INSERT INTO DeptExtsSorted (Dept_Name, Extn_Name, Dept_Id, Dept_Ext) SELECT Dept_Name, Extn_Name, Dept_Id, Dept_Ext FROM DeptExts ORDER BY Dept_Id, Extn_Name;
DROP TABLE DeptExts;
ALTER TABLE DeptExtsSorted RENAME TO DeptExts;

- you'll need to  check that the DeptExtsSorted table is created to match DeptExts, and check I've got the column names right too.
This creates a new table, sorts the data and inserts it into that new table, deletes the original, renames the sorted table to replace the original.

It's a bit warped, but appears to work ;-)

0
 

Author Comment

by:kalbarriman
ID: 33733425
Thanks Jon. Yes, I was thinking along similar lines (except I was going to suck out the data into an array, and then delete all the entries, and then re-insert them in sorted order), but I think your solution is better.
Apppreciate it. I'll close the Question now.
Andrew
0
 

Author Closing Comment

by:kalbarriman
ID: 33733430
There doesn't appear to be any way to do what I want with a single SQL command, but the solution resolves the issue in as good a way as possible.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question