Solved

Sort query with nulls at the end

Posted on 2014-07-21
6
210 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
Comment Utility
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
Comment Utility
Hi mbizup
Home now.  Will try in the morning.
Thanks
Richard
0
 

Author Closing Comment

by:rltomalin
Comment Utility
Hi mbizup

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

Regards

Richard
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now