22,210
社区成员
发帖
与我相关
我的任务
分享
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([A1] varchar(1),[A2] int)
insert [tb]
select 'B',1 union all
select 'H',1 union all
select 'A',3 union all
select 'F',3 union all
select 'J',3 union all
select 'D',1 union all
select 'C',2 union all
select 'E',2
select *
from tb
order by (
select min(A1)
from tb t
where tb.A2=t.A2),
A2,A1
/*
A1 A2
---- -----------
A 3
F 3
J 3
B 1
D 1
H 1
C 2
E 2
(8 行受影响)
*/
--学习FC 写了一个
----------------------------------------------------------------
-- Author :fredrickhu(小F 向高手学习)
-- Date :2009-07-07 21:14:48
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([A1] varchar(1),[A2] int)
insert [tb]
select 'B',1 union all
select 'H',1 union all
select 'A',3 union all
select 'F',3 union all
select 'J',3 union all
select 'D',1 union all
select 'C',2 union all
select 'E',2
--------------开始查询--------------------------
select A1,a.A2 from tb a,
(select min(a1) c ,a2 from tb group by a2)b
where a.a2=b.a2
order by c,a1,a.a2
----------------结果----------------------------
/*
A1 A2
---- -----------
A 3
F 3
J 3
B 1
D 1
H 1
C 2
E 2
(所影响的行数为 8 行)
*/
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([A1] varchar(1),[A2] int)
go
insert [tb]
select 'B',1 union all
select 'H',1 union all
select 'A',3 union all
select 'F',3 union all
select 'J',3 union all
select 'D',1 union all
select 'C',2 union all
select 'E',2
go
select a.* from tb a
inner join
(
select min(a1) ma ,a2 from tb group by a2
) b
on a.a2=b.a2
order by ma,a1,a.a2
/*
A 3
F 3
J 3
B 1
D 1
H 1
C 2
E 2
*/
----------------------------------------------------------------
-- Author :fredrickhu(小F 向高手学习)
-- Date :2009-07-07 20:44:48
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([A1] varchar(1),[A2] int)
insert [tb]
select 'B',1 union all
select 'H',1 union all
select 'A',3 union all
select 'F',3 union all
select 'J',3 union all
select 'D',1 union all
select 'C',2 union all
select 'E',2
--------------开始查询--------------------------
SELECT A1,A2
FROM tb
GROUP BY A2,A1
ORDER BY A2 desc
----------------结果----------------------------
/*
A1 A2
---- -----------
A 3
F 3
J 3
C 2
E 2
B 1
D 1
H 1
(所影响的行数为 8 行)
*/
drop table tb
SELECT *
FROM TB
ORDER BY A2,A1
declare @ta table(a1 varchar(10),a2 int)
insert into @ta
select 'B',1 union all
select 'H',1 union all
select 'A',3 union all
select 'F',3 union all
select 'J',3 union all
select 'D',1 union all
select 'C',2 union all
select 'E',2
SELECT * FROM @TA ORDER BY A2,A1
SELECT A1,A2
FROM LI
GROUP BY A2,A1
ORDER BY A1