select
id,max(time) as time
from
(select id,time1 as time from A
union all
select id,time2 as time from A
union all
select id,time3 as time from A
union all
select id,time4 as time from A) t
group by
id
id,max(time) as time
from
(select id,time1 as time from A
union all
select id,time2 as time from A
union all
select id,time3 as time from A
union all
select id,time4 as time from A) t
group by
id
解决方案 »
- htl258(tony)来帮忙!
- 新人求大仙赐教<SQL DERVER2008中存储过程,触发器,视图,游标的实例代码>
- 连本地数据库连不上。
- 请问SQL Server 2005用户的读写权限怎么设置?
- 两台服务器做双机热备用dataware,另一个台做订阅服服务器
- 高难问题,列出年份。分不够再加
- 在proxy server中想用数据库来存放缓冲的web对象,欢迎大家对表的字段和结构提出您的看法
- SQL,随机出现50-100这51个数,其中的一个,谢谢~
- 怎么删除日记文件?
- 我想学存储过程,请给我指一条学习捷径吧?
- 用Bule Insert的格式文件批量导入数据的问题,在线等
- 一个纵向表条件查询的问题
select time1 [time] from A where id=@id union all
select time2 from A where id=@id union all
select time3 from A where id=@id union all
select time4 from A where id=@id )a
select id,[time]=(select distinct max(time) from (select time1 from A where id=b.id union all select time2 from A where id=b.id union all select time3 from A where id=b.id union all select time4 from A where id=b.id) c) b
id,max(time) as time
from
(select id,time1 as time from A
union all
select id,time2 as time from A
union all
select id,time3 as time from A
union all
select id,time4 as time from A) t
group by
id----------------------------------------------------
赞一个!libin_ftsafe(子陌红尘)
create table maxTime(
id int,
time1 datetime,
time2 datetime,
time3 datetime,
time4 datetime
)--测试数据
insert into maxTime
select 1,'9:01','9:02','9:03','9:04'
union all
select 2,'10:01','10:02','10:03','10:04'
union all
select 3,'11:01','11:02','11:03','11:04'--测试
declare @id int
set @id=2
select max(t) as MaxTime from (
select [time1] as t from maxTime where id=@id
union all
select [time2] as t from maxTime where id=@id
union all
select [time3] as t from maxTime where id=@id
union all
select [time4] as t from maxTime where id=@id
) tempTable--删除表
drop table maxTime
id int,
time1 datetime,
time2 datetime,
time3 datetime,
time4 datetime
) --测试数据
insert into maxTime
select 1,'9:01','9:02','9:03','9:04'
union all
select 2,'10:01','10:02','10:03','10:04'
union all
select 3,'11:01','11:02','11:03','11:04' select id,(select distinct max(time1) from (select time1 from maxtime where id=b.id union all select time2 from maxtime where id=b.id union all select time3 from maxtime where id=b.id union all select time4 from maxtime where id=b.id) c) from maxtime bid
----------- -----------------------
1 1900-01-01 09:04:00.000
2 1900-01-01 10:04:00.000
3 1900-01-01 11:04:00.000(3 行受影响)
id int,
time1 datetime,
time2 datetime,
time3 datetime,
time4 datetime
) --测试数据
insert into maxTime
select 1,'9:01','9:02','9:03','9:04'
union all
select 2,'10:01','10:02','10:03','10:04'
union all
select 3,'11:01','11:02','11:03','11:04' select id,(select distinct max(time1) from (select time1 from maxtime where id=b.id union all select time2 from maxtime where id=b.id union all select time3 from maxtime where id=b.id union all select time4 from maxtime where id=b.id) c) from maxtime bselect
id,max(time) as time
from
(select id,time1 as time from maxtime
union all
select id,time2 as time from maxtime
union all
select id,time3 as time from maxtime
union all
select id,time4 as time from maxtime) t
group by
id好像我的快点哦
select id,
case when
(case when time1 > time2 then time1 else time2 end) >
(case when time3 > time4 then time2 else time4 end)
then
(case when time1 > time2 then time1 else time2 end)
else
(case when time3 > time4 then time3 else time4 end)
end
from A
returns datetime
as
begin
declare @t datetime
set @t = @t1
if @t < @t2 set @t = @t2
if @t < @t3 set @t = @t3
if @t < @t4 set @t = @t4
return @t
end
select id,getmax(time1,time2,time3,time4) from A