设有字段a,b.让sql语句以字段a排序,但如果某一条数据的字段a的值为空,则将这条数据排到字段b的值相同的数据下面。
2条回答 默认 最新
- juzimeizi 2016-11-01 11:34关注
select (case
when C.c = 1 then
null
when C.c = 0 then
C.a
end) a,
C.b
from (select A.*, 0 c
from A
where A.a is not null
union all
select (select min(a2.a) from A a2 where a2.b = a1.b) a, a1.b, 1 c
from A a1
where a1.a is null) C
order by C.a, C.b, C.c;可能会有些许的问题,因为b字段可能会有重复值,我取得的是a字段的最小值
解决 无用评论 打赏 举报