473,394 Members | 1,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

I guess this is a relatively simple SELECT order by question

I'm just trying to order some data correctly. In that respect, I've got the following SELECT statement...

SELECT * FROM BKMASTER WHERE MATKEY='MCCOECO' ORDER BY MEDSTA ASC

The thing is, there are only about 5 two-digit options for MEDSTA: NULL, "OE" "OP" "RT", etc. I want any records with blank MEDSTAs to come first, then records where MEDSTA='RT' to come second, then whatever else. I've tried a few different things, but who am I fooling? I don't have a clue how to do this!

Much appreciated.
Apr 11 '08 #1
4 1057
Delerna
1,134 Expert 1GB
So you need to sort in descending order but thye problem with that is the null's come at the end and you want them at the top.

Try this
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM BKMASTER 
  3. WHERE MATKEY='MCCOECO' 
  4. ORDER BY isnull(MEDSTA,'zzzzzz') ASC
  5.  
Apr 11 '08 #2
ck9663
2,878 Expert 2GB
try this:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM BKMASTER 
  2. WHERE MATKEY='MCCOECO' 
  3. ORDER BY 
  4. case 
  5.    when MEDSTA  is null then space(5) 
  6.    when MEDSTA = 'RT' then '0' + MEDSTA 
  7.    else  '1' + MEDSTA end ASC
  8.  
-- CK
Apr 12 '08 #3
delerna,

You that's right, but see I need to order them by NULL first, then by which ones say "RT" after than, then by "OE", then whatever's left...

I tried your code (although like I said I only know the real basics so I wasn't entirely sure what to put in place of the ZZZZs) and it threw an "unknown token" code right after isnull. It didn't recognize the (
Apr 12 '08 #4
ck9663
2,878 Expert 2GB
delerna,

You that's right, but see I need to order them by NULL first, then by which ones say "RT" after than, then by "OE", then whatever's left...

I tried your code (although like I said I only know the real basics so I wasn't entirely sure what to put in place of the ZZZZs) and it threw an "unknown token" code right after isnull. It didn't recognize the (
You have to sure which one would you want as top 3.

Null, RT and OE then the rest?
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM BKMASTER 
  2. WHERE MATKEY='MCCOECO' 
  3. ORDER BY 
  4. case 
  5.    when MEDSTA  is null then space(5) 
  6.    when MEDSTA = 'RT' then '0' + MEDSTA 
  7.    when MEDSTA = 'OE' then '1' + MEDSTA 
  8.    else  '2' + MEDSTA end ASC
Apr 12 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: LRW | last post by:
Having a problem getting a onSubmit function to work, to where it popsup a confirmation depending on which radiobutton is selected. Here's what I have: function checkdel() { if...
10
by: TokiDoki | last post by:
Hello there, I have been programming python for a little while, now. But as I am beginning to do more complex stuff, I am running into small organization problems. It is possible that what I...
5
by: Tim::.. | last post by:
Can someone tell me how I convert this simple SQL statement so I can use it in ASP.NET??? I have an issue with the quotation marks and wondered if there is a simple rule for converting the sql...
7
by: Scott Frankel | last post by:
Still too new to SQL to have run across this yet ... How does one return the latest row from a table, given multiple entries of varying data? i.e.: given a table that looks like this: color...
20
by: Brian Tkatch | last post by:
An ORDER BY a simple-integer inside a FUNCTION, results in SQL0440N, unless the FUNCTION expects an INTEGER as its parameter. For example: DECLARE GLOBAL TEMPORARY TABLE A(A CHAR(1)) INSERT INTO...
15
by: gjoneshtfc | last post by:
Hello, I have a simple problem that I just cannot get my head around! I currently have the following line in my ASP recordset: Recordset1.Source = "SELECT * FROM MainTable ORDER BY Price ASC"...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
1
by: Ganesh Muthuvelu | last post by:
Hello all, I have a XML as below: ****************** <BLOCK> <ROW1> <A>1</A> <B>2</B> </ROW1> <ROW2>
2
by: SoulScriber | last post by:
This comes to be an IE vs. Firefox issue. What I'm trying to do is 'onchange' of a checkbox have a table display underneath. Firefox works fine (when you click on the 'Check Here if your...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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...

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.