34,587
社区成员
发帖
与我相关
我的任务
分享
select (case when a >= b then a else b end) as maxnum
from (
select max(startnum) a, max(endnum) b
from tb
) c
select max(p) as maxnum
from (
select max(col1) a, max(col2) b, max(col3) c
from tb
) t1
unpivot (
p for i in ([a],[b],[c])
) as t2
select
max(num) as maxmun
from(
select startnum as num from tb
union all
select isnull(endnum,0) from tb
) as t
应该要考虑结束编号的空值。--自己选择
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-08-29 12:01:56
-- Verstion:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([startnum] int,[endnum] int)
insert [tb]
select 1,100 union all
select 101,null union all
select 102,null union all
select 103,555
--------------开始查询--------------------------
1.select
max(num) as maxmun
from
(
select startnum as num from tb
union all
select endnum as num from tb
) t
2.select
max(startnum) as maxstartnum,
max(endnum) as maxendnum
from
tb
----------------结果----------------------------
/* maxmun
-----------
555
警告: 聚合或其他 SET 操作消除了空值。
(1 行受影响)
maxstartnum maxendnum
----------- -----------
103 555
警告: 聚合或其他 SET 操作消除了空值。
(1 行受影响)
*/
select
max(startnum) as maxstartnum,
max(endnumber) as maxendnum
from
tb
--树哥的
select
max(num) as maxmun
from
(
select startnum as num from tb
union all
select endnum as num from tb
) t
select
mx(num) as maxmun
from
(
select startnum as num from tb
union all
select endnum from tb
) t
select max(endnum) from biao