473,499 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting so 0 is at the bottom

Is there a way to sort records the following way.

Original Table:

KeyField Column 1
1 0
2 500
3 200
4 300
5 0
To this sorted table

KeyField Column 1
3 200
4 300
2 500
1 0
5 0
so that the numbers in column 1 are sorted asc but it treats a 0 as
the largest number. We need the first number greater than 0 to be
first in the table.
Jul 20 '05 #1
5 4859

"Philip Mette" <ph******@msn.com> wrote in message
news:96**************************@posting.google.c om...
Is there a way to sort records the following way.

Original Table:

KeyField Column 1
1 0
2 500
3 200
4 300
5 0
To this sorted table

KeyField Column 1
3 200
4 300
2 500
1 0
5 0
so that the numbers in column 1 are sorted asc but it treats a 0 as
the largest number. We need the first number greater than 0 to be
first in the table.


This is one way:

select *
from dbo.MyTable
order by case Column1 when 0 then 1 else 0 end, Column1

Simon
Jul 20 '05 #2
On 25 Aug 2004 08:51:59 -0700, Philip Mette wrote:
Is there a way to sort records the following way.

Original Table:

KeyField Column 1
1 0
2 500
3 200
4 300
5 0
To this sorted table

KeyField Column 1
3 200
4 300
2 500
1 0
5 0
so that the numbers in column 1 are sorted asc but it treats a 0 as
the largest number. We need the first number greater than 0 to be
first in the table.


Hi Philip,

ORDER BY CASE Column1
WHEN 0 THEN 0
ELSE 1
END, Column1

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #3
On Wed, 25 Aug 2004 20:59:31 +0200, Hugo Kornelis wrote:
Hi Philip,

ORDER BY CASE Column1
WHEN 0 THEN 0
ELSE 1
END, Column1

Best, Hugo


Oops. Last line shopuld have been
END DESC, Column1

Of course, Simon's suggestion works okay as well.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #4
Hugo Kornelis <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message news:<h1********************************@4ax.com>. ..
On Wed, 25 Aug 2004 20:59:31 +0200, Hugo Kornelis wrote:
Hi Philip,

ORDER BY CASE Column1
WHEN 0 THEN 0
ELSE 1
END, Column1

Best, Hugo


Oops. Last line shopuld have been
END DESC, Column1

Of course, Simon's suggestion works okay as well.

Best, Hugo


Thanks Hugo works great! How is this working? Are you setting the
order index to 0 when 0 and all other numbers index to 1? I am sort of
a newbie.
Jul 20 '05 #5
On 26 Aug 2004 08:07:04 -0700, Philip Mette wrote:
Hugo Kornelis <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message news:<h1********************************@4ax.com>. ..
On Wed, 25 Aug 2004 20:59:31 +0200, Hugo Kornelis wrote:
>Hi Philip,
>
>ORDER BY CASE Column1
> WHEN 0 THEN 0
> ELSE 1
> END, Column1
>
>Best, Hugo


Oops. Last line shopuld have been
END DESC, Column1

Of course, Simon's suggestion works okay as well.

Best, Hugo


Thanks Hugo works great! How is this working? Are you setting the
order index to 0 when 0 and all other numbers index to 1? I am sort of
a newbie.


Hi Philip,

I use two sort columns. The first is
CASE Column1
WHEN 0 THEN 0
ELSE 1
END
This CASE expression will return 0 is Column1 is zero; it returns 1 in all
other cases (copy and paste this expression in the SELECT list as well if
you want to see it in action). I specified descending sort on this column
(the keyword DESC that I forgot in my earlier post), so all rows with 1
for this expression (ie all rows with Column1 <> 0) go first and all rows
with 0 for this CASE expression (ie all rows Column1 = 0) come last.

The second sort column is Column1 (the comma after DESC indicates that
another sort column follows). This one sorts in the default ASCending
order. Since it's the second sort column, it will be used as a tie
breaker. Remember, the first sort column was 1 for all rows with Column1
<> 0; all these rows go first, but their ordering withing this group will
be an ascending order based on Column1. The second group will be the
columns with 0 for the CASE expression; these all have Column1 equal to 0
so their ordering within the group is still undefined.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1144
by: reclusive monkey | last post by:
I copied the following example from MSDN, and it works nicely. However, I want to combine it with another technique I discovered on the IBM Developer site. However, the first method relies upon...
20
4020
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
23
6275
by: yatindran | last post by:
hai this is my 2d array. int a = { {5,2,20,1,30,10}, {23,15,7,9,11,3}, {40,50,34,24,14,4}, {9,10,11,12,13,14}, {31,4,18,8,27,17}, {44,32,13,19,41,19}, {1,2,3,4,5,6},
7
10071
by: Foodbank | last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've gotten some of it coded except for the actual sorting :( The book I'm teaching myself with (Data Structures Using C and...
19
25423
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
11
2159
by: Registered User | last post by:
What is the name of the following sorting method? for (i=0; i<n-1; i++) for (j=i+1; j<n; j++) if (a>a) swap(&a, &a); It seems to be the opposite of bubble sort, with lighter elements going...
1
3855
by: jjjoic | last post by:
Hi, I use Access 2003 to generate the back-end data for a ColdFusion report at work. The report is sorted by a column and based on the sorting, rankings are assigned to each row(i.e. the biggest...
2
2258
by: Steve the Pocket | last post by:
I'm writing a heapsort program. It asks for a value for "max" and creates an array of "max" # random numbers from 1-100, and then enqueues them all into a Priority Queue. Then it asks if you want...
2
2236
by: Bob Laubla | last post by:
Hello I have a very complex maketable query with many records and involving multiple VB functions which call other functions. I need this table to be sorted by the first field. But no matter what...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5479
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
4920
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.