从五个日期字段中,依次从最早的日期开始排列到最晚的日期, 不管是哪个字段,只早日期比其它字段日期早就往前 排
如果第二条记录第二个字段日期比第一条记录的 第一个字段日期早那么就把第二条记录排在第一条前面,依次类推
如果第二条记录第二个字段日期比第一条记录的 第一个字段日期早那么就把第二条记录排在第一条前面,依次类推
解决方案 »
- openrowset的问题???????????
- 如何快速將查詢到數據庫中的記錄插入記錄到另一表中?
- 请问这样的嵌套应该怎么写?
- 求一语句
- 对于经常查询的表,表的记录一般到多少条就需要建立索引了?
- 什么是SQL Server ?
- 求救~~ 公司服务器安装SQL失败~!
- 项目要求分类排序查询并要求只显示每一类前10条记录~SQL怎么写?
- 请问为什么我的MS SQL SERVER7中的Enterprise Manager里Console Root下的DataBases中的一些数据库在左边的树形图中颜色变灰了?更气人的是那些库里连数据表等等什么也没有啦!快救救我吧!!
- wps+vba+sql做报表系统,主机上运行没问题,客户端运行出错
- 数据恢复(数据库中更改了一列的数据还能把这一列的数据还原到没有更改前吗)
- 这个语句,执行就会死锁,帮看下可不可以优下
;with ach as
(
select id,date1 as date from tb
union all
select id,date2 from tb
union all
select id,date3 from tb
union all
select id,date4 from tb
union all
select id,date5 from tb
),art as
(
select *,rid=row_number() over (partition by id order by (case when date is null then 0 else date end),date)
from ach
)select id,
max(case when rid=1 then date else null end) date1,
max(case when rid=2 then date else null end) date2,
max(case when rid=3 then date else null end) date3,
max(case when rid=4 then date else null end) date4,
max(case when rid=5 then date else null end) date5
from art
group by id
DBO.GetMinDate(col1,col2,col3,col4)
比较后返回最小值select * from tablename order by dbo.GetMinDate(col1,col2,col3,col4)
select 日期1 as 日期 from tb
union
select 日期2 from tb
union
select 日期3 from tb
union
select 日期4 from tb
union
select 日期5 from tb order by 1
@date4 @datetime,@date5 @datetime)
return @datetime
as
begin
select @date1=(case when @date1<@date2 then @date2 else @date1 end)
select @date1=(case when @date1<@date3 then @date3 else @date1 end)
select @date1=(case when @date1<@date4 then @date4 else @date1 end)
select @date1=(case when @date1<@date5 then @date5 else @date1 end)
return @date1
endselect * from tb order by dbo.f_name(日期1,日期2,日期3,日期4,日期5)
@date4 datetime,@date5 datetime)
return datetime
as
begin
select @date1=(case when @date1<@date2 then @date2 else @date1 end)
select @date1=(case when @date1<@date3 then @date3 else @date1 end)
select @date1=(case when @date1<@date4 then @date4 else @date1 end)
select @date1=(case when @date1<@date5 then @date5 else @date1 end)
return @date1
endselect * from tb order by dbo.f_name(日期1,日期2,日期3,日期4,日期5)
order by 含有最小日期的列名,含有第二个最小日期的列名,含有......
--sql 2000select id ,
max(case px when 1 then c else null end) c1,
max(case px when 2 then c else null end) c2,
max(case px when 3 then c else null end) c3,
max(case px when 4 then c else null end) c4,
max(case px when 5 then c else null end) c5
from
(
select m.* , px = (select count(1) from
(
select id , c1 c from tb
union all
select id , c2 c from tb
union all
select id , c3 c from tb
union all
select id , c4 c from tb
union all
select id , c5 c from tb
) n where n.id = m.id and n.c < m.c) + 1 from
(
select id , c1 c from tb
union all
select id , c2 c from tb
union all
select id , c3 c from tb
union all
select id , c4 c from tb
union all
select id , c5 c from tb
) m
) t
group by id--sql 2005
select id ,
max(case px when 1 then c else null end) c1,
max(case px when 2 then c else null end) c2,
max(case px when 3 then c else null end) c3,
max(case px when 4 then c else null end) c4,
max(case px when 5 then c else null end) c5
from
(
select m.* , px = row_number() over(partition by id , order by c) from
(
select id , c1 c from tb
union all
select id , c2 c from tb
union all
select id , c3 c from tb
union all
select id , c4 c from tb
union all
select id , c5 c from tb
) m
) t
group by id
create function f_name(@date1 datetime,@date2 datetime,@date3 datetime,
@date4 datetime,@date5 datetime)
return datetime
as
begin
select @date1=(case when @date1>@date2 then @date2 else @date1 end)
select @date1=(case when @date1>@date3 then @date3 else @date1 end)
select @date1=(case when @date1>@date4 then @date4 else @date1 end)
select @date1=(case when @date1>@date5 then @date5 else @date1 end)
return @date1
endselect * from tb order by dbo.f_name(日期1,日期2,日期3,日期4,日期5)