Solved

Sort query with nulls at the end

Posted on 2014-07-21
6
213 Views
Last Modified: 2014-07-22
I have a query that sorts records ascending by date.  However, some of the date fields can be null, and I need these to come at the end of the list (currently they come at the start).

Here is my query:
SELECT tblClients.[Client ID], tblJobs.[Short description], Sum(tblJobVolLink.HoursWorked) AS VolHrsWorked, tblJobs.[Actual supervisor hours], tblJobs.[Date completed]
FROM (tblClients INNER JOIN tblJobs ON tblClients.[Client ID] = tblJobs.[Client ID]) LEFT JOIN tblJobVolLink ON tblJobs.[Job ID] = tblJobVolLink.[Job ID]
GROUP BY tblClients.[Client ID], tblJobs.[Short description], tblJobs.[Actual supervisor hours], tblJobs.[Date completed]
ORDER BY tblJobs.[Date completed];

I cannot figure out how I can do this.

Regards

Richard
0
Comment
Question by:rltomalin
  • 3
  • 2
6 Comments
 
LVL 84
ID: 40208951
Have you tried a Descending sort:

SELECT tblClients.[Client ID], tblJobs.[Short description], Sum(tblJobVolLink.HoursWorked) AS VolHrsWorked, tblJobs.[Actual supervisor hours], tblJobs.[Date completed]
FROM (tblClients INNER JOIN tblJobs ON tblClients.[Client ID] = tblJobs.[Client ID]) LEFT JOIN tblJobVolLink ON tblJobs.[Job ID] = tblJobVolLink.[Job ID]
GROUP BY tblClients.[Client ID], tblJobs.[Short description], tblJobs.[Actual supervisor hours], tblJobs.[Date completed]
ORDER BY tblJobs.[Date completed] DESC
0
 
LVL 61

Expert Comment

by:mbizup
ID: 40208953
Create a field to handle the Sort Order, and assign nulls a large value, using NZ:


SELECT tblClients.[Client ID], tblJobs.[Short description], Sum(tblJobVolLink.HoursWorked) AS VolHrsWorked, tblJobs.[Actual supervisor hours], tblJobs.[Date completed], NZ(tblJobs.[Date completed]
, #1/1/2300#) AS DateCompletedOrderBy
FROM (tblClients INNER JOIN tblJobs ON tblClients.[Client ID] = tblJobs.[Client ID]) LEFT JOIN tblJobVolLink ON tblJobs.[Job ID] = tblJobVolLink.[Job ID]
GROUP BY tblClients.[Client ID], tblJobs.[Short description], tblJobs.[Actual supervisor hours], tblJobs.[Date completed]
ORDER BY  NZ(tblJobs.[Date completed]
, #1/1/2300#);

Open in new window

0
 

Author Comment

by:rltomalin
ID: 40209144
Scott - that would just reverse the sort order - the dates would be sorted descending - which I don't want.

mbizup
I tried your solution but it didn't seem to work.  Is there a problem with the date format?
I have attached the results with your code (+ the option omitting the #) but that doesn't work either.

I notice that when I show the sort field it is left aligned, whereas the actual date field is right aligned.  Is this a clue to the problem?

Regards
Date-sort-code.docx
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 40209259
Try this - using CDate on the order by field:

SELECT tblClients.[Client ID], tblJobs.[Short description], Sum(tblJobVolLink.HoursWorked) AS VolHrsWorked, tblJobs.[Actual supervisor hours], tblJobs.[Date completed], CDate(NZ(tblJobs.[Date completed]
, #1/1/2300#)) AS DateCompletedOrderBy
FROM (tblClients INNER JOIN tblJobs ON tblClients.[Client ID] = tblJobs.[Client ID]) LEFT JOIN tblJobVolLink ON tblJobs.[Job ID] = tblJobVolLink.[Job ID]
GROUP BY tblClients.[Client ID], tblJobs.[Short description], tblJobs.[Actual supervisor hours], tblJobs.[Date completed]
ORDER BY  CDate(NZ(tblJobs.[Date completed]
, #1/1/2300#));

Open in new window



Or alternatively this -- formatting a date string as yyyymmdd:


SELECT tblClients.[Client ID], tblJobs.[Short description], Sum(tblJobVolLink.HoursWorked) AS VolHrsWorked, tblJobs.[Actual supervisor hours], tblJobs.[Date completed], Format(NZ(tblJobs.[Date completed]
, #1/1/2300#),'yyyymmdd')  AS DateCompletedOrderBy
FROM (tblClients INNER JOIN tblJobs ON tblClients.[Client ID] = tblJobs.[Client ID]) LEFT JOIN tblJobVolLink ON tblJobs.[Job ID] = tblJobVolLink.[Job ID]
GROUP BY tblClients.[Client ID], tblJobs.[Short description], tblJobs.[Actual supervisor hours], tblJobs.[Date completed]
ORDER BY  Format(NZ(tblJobs.[Date completed]
, #1/1/2300#), 'yyyymmdd');

Open in new window

0
 

Author Comment

by:rltomalin
ID: 40209349
Hi mbizup
Home now.  Will try in the morning.
Thanks
Richard
0
 

Author Closing Comment

by:rltomalin
ID: 40211159
Hi mbizup

Excellent.  Does just what it says on the tin!!

Regards

Richard
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

839 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