Solved

order by when there is nothing natural to order by

Posted on 2015-02-19
6
91 Views
Last Modified: 2015-02-20
My sample data looks something like this


1 aaaa
2 zzzz
3 dddd

there is not a natural way of ordering so i can't order by the id or the name.  Theirfore, my query needs to determine the order so it would returns something like

2 zzz
1 aaa
3 ddd

Where i would determine in my SQL what the order should be/
0
Comment
Question by:vbnetcoder
[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
6 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40619819
>it would returns something like
Define for us the logic behind how that sort order is achieved.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40619822
Add a new column (SortOrder Varchar(5)) where you can enter 01, 02, etc. to sort your data using this column.

Mike
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40619825
WIthout any further details, you can use CASE in an ORDER BY clause to pull off a custom sort order.
For example...
CREATE TABLE #tmp (val varchar(10)) 
INSERT INTO #tmp (val) 
VALUES ('1 aaa'), ('2 zzz'), ('3 ddd'), ('goo'), ('foo'), ('boo')

SELECT val
FROM #tmp
ORDER BY CASE LEFT(val, 1) WHEN '2' THEN 1 WHEN '1' THEN 2 WHEN '3' THEN 3 ELSE 4 END

Open in new window


For more reading check out my article on SQL Server CASE Solutions, scroll down about halfway.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40619859
'order by' works with some column or field in the result set.  Either method above would work Ok.  The biggest problem is that if you are going to use some order that is not numeric or alpha, then you have to manually define that order and find a way to use it.  On a large table, that can get pretty time consuming if it changes very often.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40620187
well I don't agree there isn't a "natural way to order" that sample data,
  it would either be numeric by id:  ORDER BY ID
  or alphabetic by name then numeric by id: ORDER BY NAME, ID

what you fail to explain at all is the "unnatural" method of order that results in:

2 zzz
1 aaa
3 ddd

that is neither numeric nor alphabetic, and without any explained logic

perhaps: ORDER BY CHECKSUM(NEWID())

(a random order)
using this 4 times produced these results:
| ID | NAME |
|----|------|
|  3 | dddd |
|  1 | aaaa |
|  2 | zzzz |


| ID | NAME |
|----|------|
|  2 | zzzz |
|  1 | aaaa |
|  3 | dddd |


| ID | NAME |
|----|------|
|  1 | aaaa |
|  2 | zzzz |
|  3 | dddd |


| ID | NAME |
|----|------|
|  1 | aaaa |
|  3 | dddd |
|  2 | zzzz |

Open in new window


details
[SQL Fiddle][1]

**MS SQL Server 2008 Schema Setup**:

    
    
    CREATE TABLE Table1
    	([ID] int, [Name] varchar(40))
    ;
    	
    INSERT INTO Table1
    	([ID], [Name])
    VALUES
    	(1, 'aaaa'),
    	(2, 'zzzz'),
    	(3, 'dddd')
    ;

**Query 1**:

    select
    id, name
    from table1
    
    ORDER BY CHECKSUM(NEWID())
    

**[Results][2]**:
    
    | ID | NAME |
    |----|------|
    |  3 | dddd |
    |  1 | aaaa |
    |  2 | zzzz |


**Query 2**:

    select
    id, name
    from table1
    
    ORDER BY CHECKSUM(NEWID())
    

**[Results][3]**:
    
    | ID | NAME |
    |----|------|
    |  2 | zzzz |
    |  1 | aaaa |
    |  3 | dddd |


**Query 3**:

    select
    id, name
    from table1
    
    ORDER BY CHECKSUM(NEWID())
    

**[Results][4]**:
    
    | ID | NAME |
    |----|------|
    |  1 | aaaa |
    |  2 | zzzz |
    |  3 | dddd |


**Query 4**:

    select
    id, name
    from table1
    
    ORDER BY CHECKSUM(NEWID())
    

**[Results][5]**:
    
    | ID | NAME |
    |----|------|
    |  1 | aaaa |
    |  3 | dddd |
    |  2 | zzzz |



  [1]: http://sqlfiddle.com/#!3/c8427/1

  [2]: http://sqlfiddle.com/#!3/c8427/1/0

  [3]: http://sqlfiddle.com/#!3/c8427/1/1

  [4]: http://sqlfiddle.com/#!3/c8427/1/2

  [5]: http://sqlfiddle.com/#!3/c8427/1/3

Open in new window

0
 

Author Closing Comment

by:vbnetcoder
ID: 40620948
ty
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

738 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