sql server中怎么样按非递减顺序排列查询出来的数据啊
sql server中怎么样按非递减顺序排列查询出来的数据啊
[解决办法]
order by 排序字段
默认就是升序的排序
[解决办法]
非递减?楼主啥意思,想要递增还是无序?
[解决办法]
问得很费解啊
[解决办法]
使用case when
参考:
-->生成测试数据:GOIF OBJECT_ID('TBL')IS NOT NULLDROP TABLE TBLGOCREATE TABLE TBL(日期 DATE,备注 VARCHAR(100))GOINSERT TBLSELECT '2012-03-02','B' UNION ALLSELECT '2012-03-05','C' UNION ALLSELECT '2012-03-06','D' UNION ALLSELECT '2012-03-07','E' UNION ALLSELECT '2012-03-09','F' UNION ALLSELECT '2012-03-11','G' UNION ALLSELECT '2012-03-12','H' UNION ALLSELECT '2012-03-13','I' UNION ALLSELECT '2012-03-15','J' UNION ALLSELECT '2012-03-19','K' UNION ALLSELECT '2012-03-20','L'--不使用union all;with tas(select *,case when 日期>=getdate() then 1 else 0 end as Afrom tbl)select 日期,备注 from torder by a,(case when a=1 then 日期 end),(case when a=0 then 日期 end) desc/*日期 备注2012-03-09 F2012-03-07 E2012-03-06 D2012-03-05 C2012-03-02 B2012-03-11 G2012-03-12 H2012-03-13 I2012-03-15 J2012-03-19 K2012-03-20 L*/declrae @str varchar(100)set @str=''select @str=@str+','+ltrim(id) from tblaselect * from tblbwhere id in(select id from tbla)order by charindex(','+ltrim(id),@str)