473,385 Members | 1,642 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,385 software developers and data experts.

sort null then by

42
Hello is there a way to sort a column by null or not null then by a second item.
Basically I have data for bar coding letters and 92% of the records have that data. So I want to to sort it by the bar codes then by state. Problem being barcode is a number and it does not work.sorting it that way. The null not null is the only way and I really just want to know if there is a SQL query that can do that because it will make my job a lot easier.
Sep 4 '08 #1
7 2046
Stewart Ross
2,545 Expert Mod 2GB
Hi. In a normal field sort all nulls are grouped together before any other actual values. If you need to sort in some form of custom order you can add a calculated field to your query which has an IIF like this:
Expand|Select|Wrap|Line Numbers
  1. BarcodeNull: IIF(IsNull([yourbarcodefield]), 1, 0)
The SQL for this is
Expand|Select|Wrap|Line Numbers
  1. SELECT ... IIF(IsNull([yourbarcodefield]), 1, 0) AS BarcodeNull, ...
All null barcodes will then be flagged with a 1 in this calculated field, non-nulls with 0.

-Stewart
Sep 4 '08 #2
Big X
42
The flag is working. But in the same query can I then go
order by BarcodeNull, State

It does not seem to work and asks me for a value for barcodenull. I guess its trying to order by a field that hasn't been created yet.
Sep 5 '08 #3
Big X
42
at the moment I got it working by first creating a new table with the null values then running another query to do the sorting. Would like to do it in one query if possible but if not no worries.
Sep 5 '08 #4
Stewart Ross
2,545 Expert Mod 2GB
Hi. In the Access query editor you can set the Order property of your calculated field to Ascending or Descending as appropriate.

When you assign a name to a calculated field the name is not available to other parts of the same query or SQL statement - as the new field really does not exist until after the query is run - hence the failure you experienced. Does not stop you ordering the field, though, and if you check out the SQL which Access itself produces you will find that the calculated field is in effect repeated for the ordering:

Expand|Select|Wrap|Line Numbers
  1. ... ORDER BY ..., IIF(ISNull([Yourbarcodefield]), 1, 0), ... 
-Stewart
Sep 5 '08 #5
NeoPa
32,556 Expert Mod 16PB
Without intending to contradict Stewart, I would just clarify that statement somewhat.

As I understand it, aliased fields (where a name is given to a result after the AS) are generally available within that set of SQL, but with the proviso that they are not available to clauses which are logically executed before the named result is worked out. Of course, this order is not published and may vary between versions of SQL.

I know that other SELECT results generally CAN access an aliased result by name. Generally WHERE & ORDER BY clauses are unable to.

To get past this, it is usual simply to repeat the formula to the left of the AS in place of using the name. As they are equivalent and SQL can always optimise this down anyway, this is pretty well exactly the same (though possibly less maintainable).
Sep 5 '08 #6
Stewart Ross
2,545 Expert Mod 2GB
Thanks for your clarification, NeoPa. I have myself used alias names elsewhere in Access queries with varying degrees of success. In one case a query in which a total was aliased and referred to by another calculated field worked well - until a new column was added, when the alias name suddenly became unrecognised. In other cases it simply hasn't been accepted at all (within the SELECT part). Hence why I simplified my response down to the safe option of treating the alias name as unavailable until the query is run. I did not know why Access should behave like that, and your explanation makes it much clearer!

-Stewart
Sep 5 '08 #7
NeoPa
32,556 Expert Mod 16PB
No worries Stewart.

On the brink of my mind is some other scenario to do with fields RELATED ...

STOP PRESS :

The other scenario I was trying hard to remember is the situation where a field which is involved in a WHERE clause, but also is displayed in the SELECT clause, refers to another aliased field from the SELECT clause.

This will fail to resolve as the WHERE clause needs to be processed first and the aliased field is not yet available.

Actually, this can be extrapolated from the earlier point when thinking logically, but I mention it because it's easy to miss the reason and see it as one SELECT field being unable to reference another which is aliased. I know I did for a while, until I worked out what must have been going on.
Sep 5 '08 #8

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

Similar topics

12
by: Eva | last post by:
Hi, I try to implement quick sort. I sort vectors by their first value. 10 2 3 4 9 3 5 6 10 4 5 6 must be 9 3 5 6 10 2 3 4 10 4 5 6 The prog works great on maybe 500 vectors, but I have an...
4
by: DancnDude | last post by:
I have a class that needs to have several different kinds of sorting routines on an ArrayList that it needs to conditionally do based upon the data. I have successfully created a class that...
1
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
9
by: Arjen | last post by:
Hello, Persons is a hashtable which I convert to an array. Person aPerson = new Person; Persons.Values.CopyTo( aPerson, 0 ); Now I can access the person items like aPerson.name or...
2
by: Mark | last post by:
Assume you have a strongly typed collection of a class called Person. The strongly typed collection implements IEnumerable so it can be databound to a server control like a DataGrid. The Person...
9
by: phillip.s.powell | last post by:
Ok, you have three tables. You're supposed to be able to not only sort (ORDER BY) according to a_name, no problem, but you must also have the ability to sort (ORDER BY) the relationship between...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
9
by: incredible | last post by:
how to sort link list of string
1
by: Pacific Fox | last post by:
Hi all, I have a SQL statement that allows paging and dynamic sorting of the columns, but what I can't figure out without making the SQL a dynamic string and executing it, or duplicating the SQL...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.