+ -
当前位置:首页 → 问答吧 → 特殊时间排序问题

特殊时间排序问题

时间:2011-12-19

来源:互联网

项目的会议管理模块,会议时间可能为待定、或者时间见附件这两种特殊情况,设计的时候如果会议时间为"3000-01-01"则为待定,如果为"4000-01-01"则为时间见附件。
我排序的时候,按会议时间倒序排,结果显示有不太正确
大致结果:
4000-01-01
4000-01-01
4000-01-01
4000-01-01
3000-01-01
3000-01-01
3000-01-01
3000-01-01
4000-01-01
4000-01-01
3000-01-01
3000-01-01
3000-01-01
3000-01-01
3000-01-01
3000-01-01

会出现这样的情况,不知是何原因?求教!

作者: dwarf471   发布时间: 2011-12-19

没明白意思 看楼下的

作者: szstephenzhou   发布时间: 2011-12-19

SQL code

if object_id('tb') is not null
   drop table tb
go
create table tb
(
 id int identity(1,1),
 name varchar(10)
)
go
insert into tb(name)
select '4000-01-01' union all
select '4000-01-01' union all
select '4000-01-01' union all
select '4000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '4000-01-01' union all
select '4000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01'
go
select * from tb order by case when name='3000-01-01' then 0 else 1 end desc
go
/*
楼主是这意思吗
id          name
----------- ----------
1           4000-01-01
2           4000-01-01
3           4000-01-01
4           4000-01-01
9           4000-01-01
10          4000-01-01
11          3000-01-01
12          3000-01-01
13          3000-01-01
14          3000-01-01
15          3000-01-01
16          3000-01-01
5           3000-01-01
6           3000-01-01
7           3000-01-01
8           3000-01-01

(16 行受影响)
*/

作者: pengxuan   发布时间: 2011-12-19

我的意思其实很简单,我就select × from 表 Order by time desc
按理应该显示
4000-01-01
4000-01-01
3000-01-01
3000-01-01
3000-01-01

但是我现在会如有几个4000年的时间夹杂在3000年里
不知为何!

作者: dwarf471   发布时间: 2011-12-19

SQL code

 if object_id('tb') is not null
   drop table tb
go
create table tb
(
 id int identity(1,1),
 name varchar(10)
)
go
insert into tb(name)
select '4000-01-01' union all
select '4000-01-01' union all
select '4000-01-01' union all
select '4000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '4000-01-01' union all
select '4000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01'
go
select * from tb order by name

id          name
----------- ----------
5           3000-01-01
6           3000-01-01
7           3000-01-01
8           3000-01-01
11          3000-01-01
12          3000-01-01
13          3000-01-01
14          3000-01-01
15          3000-01-01
16          3000-01-01
9           4000-01-01
10          4000-01-01
1           4000-01-01
2           4000-01-01
3           4000-01-01
4           4000-01-01

(16 行受影响)


作者: szstephenzhou   发布时间: 2011-12-19

你是怎么排序的?直接order by 时间列?

还有你的时间列是字符型的么?

作者: fredrickhu   发布时间: 2011-12-19

引用 5 楼 fredrickhu 的回复:
你是怎么排序的?直接order by 时间列?

还有你的时间列是字符型的么?


时间列是datetime类型的,直接按时间列倒序
SQL code

  select * from tb order by time desc


作者: dwarf471   发布时间: 2011-12-19

其实我自建了一测试表,实验也是没问题的,但是到正式的表里,查出来就有刚才的问题

作者: dwarf471   发布时间: 2011-12-19

引用 7 楼 dwarf471 的回复:
其实我自建了一测试表,实验也是没问题的,但是到正式的表里,查出来就有刚才的问题

正式表数据和结构贴出来

作者: pengxuan   发布时间: 2011-12-19

SQL code

declare @T table (col datetime)
insert into @T
select '4000-01-01' union all
select '4000-01-01' union all
select '4000-01-01' union all
select '4000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '4000-01-01' union all
select '4000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01' union all
select '3000-01-01'

select * from @T ORDER BY 1 DESC
/*
col
-----------------------
4000-01-01 00:00:00.000
4000-01-01 00:00:00.000
4000-01-01 00:00:00.000
4000-01-01 00:00:00.000
4000-01-01 00:00:00.000
4000-01-01 00:00:00.000
3000-01-01 00:00:00.000
3000-01-01 00:00:00.000
3000-01-01 00:00:00.000
3000-01-01 00:00:00.000
3000-01-01 00:00:00.000
3000-01-01 00:00:00.000
3000-01-01 00:00:00.000
3000-01-01 00:00:00.000
3000-01-01 00:00:00.000
3000-01-01 00:00:00.000
*/

作者: maco_wang   发布时间: 2011-12-19

你把所谓的正式表中的数据弄点出来让大伙看看如何.

作者: qianjin036a   发布时间: 2011-12-19

SQL code

CREATE TABLE [dbo].[wd_Meeting_Notice](
    [BelongXiaQuCode] [nvarchar](50) NULL,
    [OperateUserName] [nvarchar](50) NULL,
    [OperateDate] [datetime] NULL,
    [Row_ID] [int] IDENTITY(1,1) NOT NULL,
    [YearFlag] [nvarchar](4) NULL,
    [RowGuid] [nvarchar](50) NULL,
    [MeetingTitle] [nvarchar](500) NULL,
    [MeetingDate] [nvarchar](50) NULL,
    [MeetingHour] [nvarchar](50) NULL,
    [MeetingMini] [nvarchar](50) NULL,
    [IfSpecific] [nvarchar](50) NULL,
    [MeetingBeginTime] [nvarchar](500) NULL,
    [Address] [nvarchar](500) NULL,
    [ChuXiRenY] [ntext] NULL,
    [HuiYi_YC] [ntext] NULL,
    [ReMark] [nvarchar](2000) NULL,
    [NoticePerson] [ntext] NULL,
    [NoticePersonGuid] [ntext] NULL,
    [SubOUName] [nvarchar](50) NULL,
    [LoadDate] [datetime] NULL,
    [ArchiveFileTypeGuid] [nvarchar](50) NULL,
    [ProcessVersionInstanceGuid] [nvarchar](50) NULL,
    [SubWebFlowOuGuid] [nvarchar](50) NULL,
    [BaseOuGuid] [nvarchar](50) NULL,
    [DelFlag] [int] NULL,
    [PaperGUID] [nvarchar](50) NULL,
    [ArchiveState] [nvarchar](50) NULL,
    [InitUserDisplayName] [nvarchar](50) NULL,
    [InitUserGuid] [nvarchar](50) NULL,
    [MeetingSet] [int] NULL,
    [txtLoadDate] [nvarchar](50) NULL,
    [MeetingTime] [datetime] NULL,
    [MeetingTimeV2] [datetime] NULL,
    [MeetingTimeV3] [nvarchar](100) NULL,
    [IsNotice] [int] NULL)


作者: dwarf471   发布时间: 2011-12-19

上面是按照MeetingTime倒序查找出来的数据

作者: dwarf471   发布时间: 2011-12-19