Solved

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

Posted on 2009-04-06
2
625 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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

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

8 Experts available now in Live!

Get 1:1 Help Now