select * from (SELECT * FROM 影片 where 主演 = '刘德华' union all SELECT * FROM 影片 where 主演 = '周星驰' ) DERIVEDTBL
order by (case when 主演='刘德华' then 0 else 1 end), 观看次数 desc
create table data(列1 char(1), 列2 char(1), 列3 int identity(1,1))
insert data select 'A', 'B'
union all select 'A', 'B'
union all select 'B', 'C'
union all select 'C', 'C'
union all select 'A', 'B'
union all select 'A', 'C'
union all select 'A', 'C'
union all select 'A', 'C'
union all select 'A', 'B'
union all select 'A', 'B'
union all select 'A', 'B'
union all select 'A', 'C'
select * from (
select top 10 * from data
order by case when 列1='A' and 列2='B'then (select count(*)from data) else 列3 end desc)a
order by 列3 desc
/*
列1 列2 列3
A C 12
A B 11
A B 10
A B 9
A C 8
A C 7
A C 6
A B 5
A B 2
A B 1
*/
declare @i as int
set @i = 0
select @i = max(列3) from tb
select top 10 * from
(
select * from tb where 列1 = 'A' and 列2 = 'B'
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
) t
order by 列3 desc
declare @i as int
set @i = 0
select @i = max(列3) from tb
select top 10 * from
(
select * from tb where 列1 = 'A' and 列2 = 'B'
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
) t
create table T(列1 char(1), 列2 char(1), 列3 int)
insert T select 'A', 'B', 10
union all select 'A', 'B', 11
union all select 'B', 'B', 12
union all select 'C', 'B', 13
union all select 'A', 'B', 14
union all select 'A', 'C', 15
go
set rowcount 4
select *, sort=1 from T
where 列1='A' and 列2='B'
union all
select *, sort=2 from T
where 列1<>'A' or 列2<>'B'
order by sort, 列3 desc
set rowcount 0
--result
列1 列2 列3 sort
---- ---- ----------- -----------
A B 14 1
A B 11 1
A B 10 1
A C 15 2