简体   繁体   English

如何按升序对列进行排序,最后将NULL值排序?

[英]How to sort a column in ascending order with NULL values at the end?

I'm working on a personal database to track my projects. 我正在使用个人数据库来跟踪我的项目。

I have a date/time column called StartDate in the table that I'm sorting in ascending order in the query that's used as the data source for the form where I'm entering data. 我在查询中按升序排序的表中有一个称为StartDate的日期/时间列,该查询用作输入数据的表单的数据源。

The form is a split form that displays the data in the "Detail" section in table format. 该表格是一个拆分表格,以表格格式显示“详细信息”部分中的数据。

Until now, I have entered projects as they are assigned, but I have a need now to also track common projects that will be assigned but have not been assigned yet. 到目前为止,我已经输入了分配的项目,但是现在我还需要跟踪将要分配但尚未分配的常见项目。

To track this, I wanted to enter the future projects without a start date. 为了对此进行跟踪,我想输入没有开始日期的未来项目。

However, when I enter the records like this, they are getting sorted to the top instead of at the bottom of the list where I prefer it. 但是,当我输入这样的记录时,它们将排在顶部,而不是我喜欢的列表的底部。

Is there any way to override the sort so the records with a value in the StartDate column with sort ascending, but the records without a value in the StartDate column will sort at the end? 有什么方法可以覆盖排序,以便StartDate列中具有值的记录以升序排列,但是StartDate列中没有值的记录将在末尾排序吗?

Thanks! 谢谢!

If you calculate anything on the date field, you'll loose the use of the index on that. 如果您在日期字段上进行任何计算,则将失去对该索引的使用。

So check for Null : 因此检查Null

SELECT 
    startdate, id
FROM 
    YourTable
ORDER BY
    StartDate Is Null,
    StartDate

A possibility would be to sort by this: 一种可能是按此排序:

ORDER BY Nz([StartDate], CDate("31.12.2999"))

Here the function NZ is used to convert Null values ('on the fly', only in the query) to the date 31.12.2999 and so the sorting is as you want. 此处,函数NZ用于将Null值(“仅在查询中”在运行中)转换为日期31.12.2999 ,因此可以根据需要进行排序。

You can create a calculated field to order your data. 您可以创建一个计算字段来排序数据。

I did a simple table (named tbl ) with dates, some of them with blanks: 我用日期创建了一个简单的表(名为tbl ),其中一些带有空格:

在此处输入图片说明

As you can see, rows with ID 4,5 and 9 are blanks in StartDate 如您所见,在StartDate ID为4,5和9的行为空白

Then I made up a query with a calculated field that will check if the field StartDate is not blank. 然后,我用计算所得的字段组成了查询,该查询将检查字段StartDate 是否不为空。 If true, return 0, if false, return 1. That way all blank Startdate rows will return 1 and non blanks will return 0. Then order by this calculated field first and order by StartDate as second order field. 如果为true,则返回0,如果为false,则返回1。这样,所有空白的Startdate行将返回1,非空白的行将返回0。然后,首先按此计算字段排序,然后按StartDate排序为第二顺序字段。

The formula of calculated field is: IIf(IsNull([StartDate])=False,0,1) 计算字段的公式为: IIf(IsNull([StartDate])=False,0,1)

The SQL code of query is: 查询的SQL代码为:

SELECT tbl.startdate, tbl.id
FROM tbl
ORDER BY IIf(IsNull([StartDate])=False,0,1), tbl.startdate

I get this as output: 我得到这个作为输出:

在此处输入图片说明

As you can see, rows with ID 4,5 and 9 are blanks in StartDate , and they are at bottom of recordset. 如您所见,ID 4,5和9的行在StartDate为空白,位于记录集的底部。

Hope you can adap this to your needs. 希望您能适应您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM