求一统计sql语句

garfieldzf 2011-09-14 04:19:20
按照sign_date 不为空的进行升序排序(不为空的排在最前),sign_date为空按照issu_date升序排序


select * from (
select '2011-09-14' as sign_date,'2011-09-01' as issu_date
union
select '2011-09-10' as sign_date,'2011-09-05' as issu_date
union
select '2011-09-12' as sign_date,'2011-09-02' as issu_date
union
select '' as sign_date,'2011-09-04' as issu_date
union
select '' as sign_date,'2011-09-05' as issu_date
) a
order by sign_date ,issu_date



目前的结果:
2011-09-04
2011-09-05
2011-09-10 2011-09-05
2011-09-12 2011-09-02
2011-09-14 2011-09-01
想要的结果:
2011-09-10 2011-09-04
2011-09-12 2011-09-05
2011-09-14 2011-09-05
2011-09-02
2011-09-01

...全文
143 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
garfieldzf 2011-09-19
  • 打赏
  • 举报
回复

select case when sign_date='' then'NULL' else sign_date end sign_date,issu_date from (

select '2011-09-14' as sign_date,'2011-09-01' as issu_date

union

select '2011-09-10' as sign_date,'2011-09-05' as issu_date
union
select '2011-09-12' as sign_date,'2011-09-02' as issu_date
union
select '' as sign_date,'2011-09-04' as issu_date
union
select '' as sign_date,'2011-09-05' as issu_date

) a

order by sign_date ,issu_date
风骑士之怒 2011-09-14
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lovesongforever 的回复:]
引用 7 楼 wknight_it 的回复:

如果就单单一句SQL,还真不知道怎么弄,继续关注


感谢你的参与, 我是用别的方法解决的。
[/Quote]
能分享下解决的办法吗?
garfieldzf 2011-09-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wknight_it 的回复:]

如果就单单一句SQL,还真不知道怎么弄,继续关注
[/Quote]

感谢你的参与, 我是用别的方法解决的。
风骑士之怒 2011-09-14
  • 打赏
  • 举报
回复
如果就单单一句SQL,还真不知道怎么弄,继续关注
风骑士之怒 2011-09-14
  • 打赏
  • 举报
回复
create table tt
(
sign_date nvarchar(20),
issu_date nvarchar(20)
)

insert into tt
select null,'2011-09-04' union all
select null,'2011-09-05' union all
select '2011-09-10','2011-09-05' union all
select '2011-09-12','2011-09-02' union all
select '2011-09-14','2011-09-01'


select * from (select top 10 * from tt where sign_date is not null order by sign_date) as a
union all
select * from (select top 10 * from tt where sign_date is null order by issu_date) as b
xfxlylove 2011-09-14
  • 打赏
  • 举报
回复
按你那样写基本不能实现,你可以用Case判断,如果为空的话你给他一个可能的最大日期。这样排序就没问题了
bdmh 2011-09-14
  • 打赏
  • 举报
回复
select * from (
select 1 as num,* from xxx where xxx is not null order by xxx
union
select 2 as num* from xxx where xxx is null order by xxx) order by num

仅供参考
迷茫的凡人 2011-09-14
  • 打赏
  • 举报
回复
来错板块了,你应该去SQL板块,那边高手多
garfieldzf 2011-09-14
  • 打赏
  • 举报
回复
结果贴得时候有问题, 我想要的是回复的这个结果。
garfieldzf 2011-09-14
  • 打赏
  • 举报
回复
想要的结果:
2011-09-10 2011-09-04
2011-09-12 2011-09-05
2011-09-14 2011-09-05
2011-09-02
2011-09-01

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧