Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sort query with nulls at the end

Posted on 2014-07-21
6
Medium Priority
?
222 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 85
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 61

Accepted Solution

by:
mbizup earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

916 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