日期:2014-05-19  浏览次数:20723 次

MAX 函数不能返回空值(NULL),如果我想返回NULL或者最大值怎么办呢?
SELECT   *   From   Table1
WHERE
      (Column1   =   SELECT   MAX(Colunm1)   FROM   Table1)   OR   (Column1   IS   NULL)


如果这样写会返回最大值和一个空值(2行结果)。如果我想返回NULL或者最大值怎么办呢?(只要一行结果)

------解决方案--------------------
SELECT top 1 *
From Table1
WHERE
(Column1 = SELECT MAX(Colunm1) FROM Table1) OR (Column1 IS NULL)


--或

set rowcount 1

SELECT * From Table1
WHERE
(Column1 = SELECT MAX(Colunm1) FROM Table1) OR (Column1 IS NULL)

------解决方案--------------------
SELECT top 1 *
From Table1
order by Column1 desc
------解决方案--------------------
select max (case when ISNUMERIC(Colunm1)=1 then Colunm1 else null) from Table1
------解决方案--------------------
create table tablenull
(
id int ,
varstr varchar(10)
)

insert into tablenull
select '5 ', 'cc ' union all select
'1 ', 'dd ' union all select
' ', 'dd '


select max (case when id =null then null else id end ) from tablenull
------解决方案--------------------
create table tablenull
(
id varchar(10),
varstr varchar(10)
)
insert into tablenull
select '5 ', 'cc ' union all select
'1 ', 'dd ' union all select
'null ', 'dd '

select max (case when id =null then null else id end ) from tablenull
------解决方案--------------------
SELECT * From Table1
WHERE
(isnull(Column1, ' ') = SELECT MAX(isnull(Colunm1, ' ')) FROM Table1)

isnull 转换成 ' ' 可以嘛?这样就可以用=号进行比较了
------解决方案--------------------
--try
select id,isnull(select max(mydate) from table2 where id=a.ID,null) as mydate
from table1 a
------解决方案--------------------
最大值:
SELECT isnull((SELECT MAX(Colunm1) FROM Table1 where table1.column1=t.Colunm1),null) as value From Table1 as t
WHERE




------解决方案--------------------

SELECT isnull((SELECT MAX(Colunm1) FROM Table1 where table1.column1=t.Colunm1),null) as value From Table1 as t

------解决方案--------------------
--這樣不是可以了嗎?
SELECT table1.id, mydate=(select max(mydate) from table2 where id=table1.id)
FROM table1 LEFT OUTER JOIN table2 ON table1.id = table2.id

------解决方案--------------------
这样试试
如果要返回最大mydate 对应 的school :
SELECT table1.id, table2_1.mydate,table2_2.school
FROM table1 LEFT OUTER JOIN
(select id,MAX(mydate) as mydate from table2 group by id ) table2_1
ON table1.id = table2_1.id
inner join table2 table2_2 ON table2_1.id = table2_2.id and table2_1.mydate = table2_2.mydate