?
Solved

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

Posted on 2009-04-06
2
Medium Priority
?
689 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Accepted Solution

by:
8080_Diver earned 2000 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

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 SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

765 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