Solved

How to sort and existing table in SQL (SMMS 2005)

Posted on 2009-04-06
2
646 Views
Last Modified: 2012-06-27
I currently have a table already in production that would like to add a sort to (GROUP BY field ASC).  The table currently is just sorted by the PK in an ascending order.  i would like to sort on another field.  

The table is used as a backend storage for a web app.  So by the sorting the table on the backend, I would like the usernames sorted on the front end web app.
0
Comment
Question by:szadroga
2 Comments
 
LVL 22

Accepted Solution

by:
8080_Diver earned 500 total points
ID: 24077667
Unless you change the primary key, which I would not advise, you cannot "sort" the table.  Also, "GROUP BY" is the wrong terminology, you should be thinking "ORDER BY".
So, essenitally, what yyou probably want to do is to use a query that looks something like the following SQL1.  Now if you want to provide some flexibility so that users can select a sort order (e.g. LastAccessDate instead of UserName), you could put your code into a stored procedure sort of like SQL_2.

SQL_1:
 
SELECT {list your columns here and don't use an asterisk}
FROM   {your table}
ORDER BY {whatever your columns are that you want the data "sorted by"}
 
 
 
SQL_2:
 
CREATE PROCEDURE dbo.SelectSortedUsers
   @SortOrder    VarChar(300)
AS
BEGIN
 
   SET NOCOUNT ON;
 
   DECLARE   @SQL_Statement   AS   VARCHAR(8000);
 
   SET @SQL_Statement = 'SELECT {list your columns here and don't use an asterisk}' +
                        'FROM   {your table} ' +
                        'ORDER BY ' + @SortOrder + ';';
 
   EXEC @SQL_Statement;
 
   SET NOCOUNT OFF
 
END

Open in new window

0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24078404
My basic question here is why you need to store the Grouped and Sorted values in the table itself.
Instead trying to sort the table you can have the current Primary Key which sorts the data physically in that table and then whenever you fetch the records from the table, query using appropriate GROUP BY and Sortings.

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to "ASSIGN" a RecordID based on 1st character of a Field? 8 36
format nvarchar field as mm/dd/yyyy 4 68
SQL Agent Timeout 5 58
Delete from table 6 47
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

785 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