Solved

Re-sorting Sqlite table

Posted on 2010-09-22
5
648 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Read about achieving the basic levels of HRIS security in the workplace.
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now