IF NOT OBJECT_ID('TEMPDB..#temp ') IS NULL DROP TABLE #temp
select N'2010年上紙數量(MT)' as f
into #temp
union all
select N'2010年領紙數量(MT)' as f
union all
select N'2009年上紙數量(MT)' as f
union all
select N'2009年領紙數量(MT)' as f
select * from #temp order by f desc執行上面的SQL,得到的結果
2010年領紙數量(MT)
2010年上紙數量(MT)
2009年領紙數量(MT)
2009年上紙數量(MT)而我想要的結果是:
2010年上紙數量(MT)
2010年領紙數量(MT)
2009年上紙數量(MT)
2009年領紙數量(MT)
請問,我該怎麼處理呢?謝謝
select N'2010年上紙數量(MT)' as f
into #temp
union all
select N'2010年領紙數量(MT)' as f
union all
select N'2009年上紙數量(MT)' as f
union all
select N'2009年領紙數量(MT)' as f
select * from #temp order by f desc執行上面的SQL,得到的結果
2010年領紙數量(MT)
2010年上紙數量(MT)
2009年領紙數量(MT)
2009年上紙數量(MT)而我想要的結果是:
2010年上紙數量(MT)
2010年領紙數量(MT)
2009年上紙數量(MT)
2009年領紙數量(MT)
請問,我該怎麼處理呢?謝謝
into #temp
union all
select N'2010年領紙數量(MT)' as f
union all
select N'2009年上紙數量(MT)' as f
union all
select N'2009年領紙數量(MT)' as fselect * from #temp order by f desc , case when charindex('上紙' , f) > 0 then 1 else 2 end
/*
f
-------------
2010年上紙數量(MT)
2010年領紙數量(MT)
2009年上紙數量(MT)
2009年領紙數量(MT)(所影响的行数为 4 行)
*/drop table #temp
(CASE WHEN CHARINDEX('上紙', f)>0 THEN REPLACE(f, '上紙', 1) ELSE REPLACE(f, '領紙', 0) END) desc
declare @table table (col varchar(18))
insert into @table
select '2010年領紙數量(MT)' union all
select '2010年上紙數量(MT)' union all
select '2009年領紙數量(MT)' union all
select '2009年上紙數量(MT)'select * from @table order by col desc,charindex('上紙',col)/*
col
------------------
2010年上紙數量(MT)
2010年領紙數量(MT)
2009年上紙數量(MT)
2009年領紙數量(MT)
*/
into #temp
union all
select N'2010年領紙數量(MT)' as f
union all
select N'2009年上紙數量(MT)' as f
union all
select N'2009年領紙數量(MT)' as fselect * from #temp order by f desc,charindex('上紙',f)
select N'2010年上紙數量(MT)' as f
into #temp
union all
select N'2010年領紙數量(MT)' as f
union all
select N'2009年上紙數量(MT)' as f
union all
select N'2009年領紙數量(MT)' as fselect * from #temp order by f desc , case when charindex('上紙' , f) > 0 then 1 else 2 end
/*
f
-------------
2010年上紙數量(MT)
2010年領紙數量(MT)
2009年上紙數量(MT)
2009年領紙數量(MT)*/drop table #temp
select N'2010年上紙數量(MT)' as f
into #temp
union all
select N'2010年領紙數量(MT)' as f
union all
select N'2009年上紙數量(MT)' as f
union all
select N'2009年領紙數量(MT)' as fselect * from #temp order by f desc , case when charindex('上紙' , f) > 0 then 1 else 2 end
/*
f
-------------
2010年上紙數量(MT)
2010年領紙數量(MT)
2009年上紙數量(MT)
2009年領紙數量(MT)(所影响的行数为 4 行)
*/drop table #temp
自己再定义一字段来排序就行了
IF NOT OBJECT_ID('TEMPDB..#temp ') IS NULL DROP TABLE #temp
select N'2010年上紙數量(MT)' as f,N'201002' as f1
into #temp
union all
select N'2010年領紙數量(MT)' as f,N'201001' as f1
union all
select N'2009年上紙數量(MT)' as f,N'200902' as f1
union all
select N'2009年領紙數量(MT)' as f,N'200901' as f1
select [f] from #temp order by [f1] desc