特殊数据排序问题

xyworkroom 2009-10-16 09:33:22
现有一张表数据如下:
ID sname orgname
1 张三 华为
2 李四 华为
3 王五 金蝶
.....

要求显示结果如下():
ID sname orgname
1 张三 华为
3 王五 金蝶
2 李四 华为


即orgname内容相同时要求分开显示。我现在做的是考试系统,要求同一单位的人分开坐。

...全文
84 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2009-10-16
  • 打赏
  • 举报
回复
你这个不加排序字段没办法做
xyworkroom 2009-10-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fredrickhu 的回复:]
SQL code----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-10-16 09:39:38
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00¡­
[/Quote]

补充说明下:我的数据库是sqlserver2000
xyworkroom 2009-10-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dawugui 的回复:]
SQL codeselect* , px= (selectcount(1)from tbwhere orgname= t.orgnameand id< t.id)+1from tborderby px , orgname
[/Quote]

这样有问题啊,如果是以下数据显示的结果就有问题了。

create table tb(ID int, sname varchar(10) , orgname varchar(10))

GO

insert into tb values(1 , '张三' , '华为')
insert into tb values(2 , '李四' , '华为')
insert into tb values(3 , '王五' , '金蝶')
insert into tb values(4 , '五月' , '华为')
insert into tb values(5 , '要求' , '中兴')
go

select * , px = (select count(1) from tb where orgname = t.orgname and id < t.id) + 1 from tb t order by px , orgname

drop table tb

--显示结果
ID sname orgname px
1 张三 华为 1
3 王五 金蝶 1
5 要求 中兴 1
2 李四 华为 2
4 五月 华为 3

--小F-- 2009-10-16
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-10-16 09:39:38
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[sname] varchar(4),[orgname] varchar(4))
insert [tb]
select 1,'张三','华为' union all
select 2,'李四','华为' union all
select 3,'王五','金蝶'
--------------开始查询--------------------------
select
ID,sname,orgname,id0
from
(
select
*,
id0=row_number() over(PARTITION BY orgname order by id)
from
[tb])t
order by
4
----------------结果----------------------------
/*ID sname orgname id0
----------- ----- ------- --------------------
1 张三 华为 1
3 王五 金蝶 1
2 李四 华为 2

(3 行受影响)
*/
SQL77 2009-10-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dawugui 的回复:]
SQL codeselect* , px= (selectcount(1)from tbwhere orgname= t.orgnameand id< t.id)+1from tborderby px , orgname
[/Quote]
顶,加个排序,再最后排序
dawugui 2009-10-16
  • 打赏
  • 举报
回复
create table tb(ID int, sname varchar(10) , orgname varchar(10))
insert into tb values(1 , '张三' , '华为')
insert into tb values(2 , '李四' , '华为')
insert into tb values(3 , '王五' , '金蝶')
go

select * , px = (select count(1) from tb where orgname = t.orgname and id < t.id) + 1 from tb t order by px , orgname

drop table tb

/*

ID sname orgname px
----------- ---------- ---------- -----------
1 张三 华为 1
3 王五 金蝶 1
2 李四 华为 2

(所影响的行数为 3 行)

*/
dawugui 2009-10-16
  • 打赏
  • 举报
回复
select * , px = (select count(1) from tb where orgname = t.orgname and id < t.id) + 1 from tb order by  px , orgname 

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧