select
*
from
T a
order by (select count(1) from T where col2=a.col2 and col1<=a.col1)
*
from
T a
order by (select count(1) from T where col2=a.col2 and col1<=a.col1)
解决方案 »
- 问一些sqlserver的基础知识,望各位朋友不吝赐教!
- 我的数据库不小心点了删除,能不能救啊?
- 求一SQL语句:
- 有无对东方通信股份了解的?公司如何?上海分公司在什么地方?
- 数据库 触发器问题 急求!!
- 连接SSIS的时候提示“RPC服务器不可用”?
- 为何在trigger里不能调用另一台机的存储过程?
- 哪里有SQL SERVER 2000的个人版下载?
- 存储过程中问题
- 最后到人才市场转了一圈(杭州),发现几乎没有单位要VC程序员,全是PB,DELPHI,VB等工具搞数据库SQL SERVER OR ORACLE等等......
- 救助SQL如何实现
- vs2005+SQL Server2000 连接问题
insert @T select 'A', 1
insert @T select 'B', 1
insert @T select 'C', 3
insert @T select 'D', 2
insert @T select 'E', 3
insert @T select 'F', 2
insert @T select 'G', 4 select
*
from
@T a
order by (select count(1) from @T where col2=a.col2 and col1 <=a.col1),col2
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)col1 col2
---------- -----------
A 1
D 2
C 3
G 4
B 1
F 2
E 3(所影响的行数为 7 行)
insert @T select 'A', 1
insert @T select 'B', 1
insert @T select 'C', 3
insert @T select 'D', 2
insert @T select 'E', 3
insert @T select 'F', 2
insert @T select 'G', 4 select
*
from
@T a
order by (select count(1) from @T where col2=a.col2 and col1 >=a.col1)desc,col2 asc(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)col1 col2
---------- -----------
A 1
D 2
C 3
B 1
F 2
E 3
G 4(所影响的行数为 7 行)
insert into tb values('A', 1 )
insert into tb values('B', 1 )
insert into tb values('C', 3 )
insert into tb values('D', 2 )
insert into tb values('E', 3 )
insert into tb values('F', 2 )
insert into tb values('G', 4 )
goselect * from tb t order by (select count(*) from tb where 列2 = t.列2 and 列1 < t.列1) + 1 , 列2drop table tb/*
列1 列2
---------- -----------
A 1
D 2
C 3
G 4
B 1
F 2
E 3(所影响的行数为 7 行)
*/
insert into tb values('A', 1 )
insert into tb values('B', 1 )
insert into tb values('C', 3 )
insert into tb values('D', 2 )
insert into tb values('E', 3 )
insert into tb values('F', 2 )
insert into tb values('G', 4 )
goselect * from tb t order by (select count(*) from tb where 列2 = t.列2 and 列1 >= t.列1) + 1 desc, 列2drop table tb/*
列1 列2
---------- -----------
A 1
D 2
C 3
B 1
F 2
E 3
G 4(所影响的行数为 7 行)
*/
declare @T table(col1 nvarchar(10), col2 int)
insert @T select 'A', 1
insert @T select 'B', 1
insert @T select 'C', 3
insert @T select 'D', 2
insert @T select 'E', 3
insert @T select 'F', 2
insert @T select 'G', 4 select * from
(select px=(select count(1) from @t where col2=a.col2 and col1>=a.col1),* from @t a)b
order by px desc,col2
如果第一列没有大小关系时,只用生成临时表增加一个自增列
2005可用row_number()over(partition by col2 order by col2)--生成序号