dz Edit id
-------------------------------------
东大街 CD001 null 1
东大街 CD001 CD009 2
东大街 CD001 CD009 3
东大街 CD001 null 4
西大街 CD002 null 5
西大街 CD001 CD184 6
西大街 CD001 null 7
南大街 CD001 null 8
南大街 CD001 null 9dz 就是地址 不为空
(标记)没有规律不为空
edit(修改后的标记) 没有规律可为空
id是唯一值的int型字段
要求:
同地址且edit全是NULL的,取id最大的那条记录的dz,,edit字段同地址且edit非NULL的,取edit非空且id最大的那条记录的dz,,edit字段查询结果应如下
东大街 CD001 CD009 3
西大街 CD001 edit1 6
南大街 CD001 null 9求此解...自己写了一条执行了十多分钟没出来
-------------------------------------
东大街 CD001 null 1
东大街 CD001 CD009 2
东大街 CD001 CD009 3
东大街 CD001 null 4
西大街 CD002 null 5
西大街 CD001 CD184 6
西大街 CD001 null 7
南大街 CD001 null 8
南大街 CD001 null 9dz 就是地址 不为空
(标记)没有规律不为空
edit(修改后的标记) 没有规律可为空
id是唯一值的int型字段
要求:
同地址且edit全是NULL的,取id最大的那条记录的dz,,edit字段同地址且edit非NULL的,取edit非空且id最大的那条记录的dz,,edit字段查询结果应如下
东大街 CD001 CD009 3
西大街 CD001 edit1 6
南大街 CD001 null 9求此解...自己写了一条执行了十多分钟没出来
select *
from tablename a
where Edit is not null
and not exists (
select 1 from tablename
where Edit is not null
and dz=a.dz
and id>a.id
)
union all
select *
from tablename a
where Edit is null
and not exists (
select 1 from tablename
where Edit is not null
and dz=a.dz
)
and not exists (
select 1 from tablename
where dz=a.dz
and id>a.id
)
有没有更快的?
要是null预先换成‘’会不会快些?更正一下
查询结果应如下
东大街 CD001 CD009 3
西大街 CD001 CD184 6
南大街 CD001 null 9
select a.*
from taa a inner join (
select dz,max( case when Edit is not null then id+1000000000 else id end ) as id
from taa
group by dz) b on a.dz=b.dz and (a.id=b.id or a.id=b.id-1000000000)--RESULT
东大街 CD001 CD009 3
南大街 CD001 NULL 9
西大街 CD001 CD184 6
还想请问是不是 判断 null值 非常消耗资源