求一条排序语句。谢谢

visualcpu 2012-10-26 10:39:25
部门ID,学习总分
1 60
1 20
1 10
2 60
2 30
3 30

要求,先部门总分高者排1名,如果部门之间的总分相同,取团队成员高分数者排前。

上面的实例数据 排名结果应该是 部门2排第一,部门1排第二 。因为部门1和部门2总分一样。但部门30分高于部门1的其它成员。

我用 group by ID,order by sum(学习总分) desc,max(学习总分) desc 没有用
...全文
338 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
visualcpu 2012-10-26
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

引用 9 楼 的回复:
引用 6 楼 的回复:

select ID,SUM(学习总分) as 总分
from TB
group by id
order by SUM(学习总分) desc ,MAX(学习总分) desc,COUNT(*)


朋友,您这个语句一看就不对。谢谢您,我们不按人数来统计。我们是取分值


晕 怎么不对啊 ?目测和执行结果看看 改下测试数据 再……
[/Quote]

兄弟,您这个肯定错的。


部门ID,学习总分
1 60
1 20
1 10
2 60
2 30
2 0
2 0
开启时代 2012-10-26
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]
引用 6 楼 的回复:

select ID,SUM(学习总分) as 总分
from TB
group by id
order by SUM(学习总分) desc ,MAX(学习总分) desc,COUNT(*)


朋友,您这个语句一看就不对。谢谢您,我们不按人数来统计。我们是取分值
[/Quote]

晕 怎么不对啊 ?目测和执行结果看看 改下测试数据 再看看 各种测试都对
visualcpu 2012-10-26
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

2000用函数

create function f_name(@id int)
returns varchar(100)
as
begin
declare @str varchar(100)
set @str='';
select @str=@str+rtrim(sc) from tb
where id=@id order by sc……
[/Quote]

非常感谢,请问,还有什么好的办法吗,不用自定义函数,就用一句sql语句解决。哪怕复杂一点都可以。
visualcpu 2012-10-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

select ID,SUM(学习总分) as 总分
from TB
group by id
order by SUM(学习总分) desc ,MAX(学习总分) desc,COUNT(*)
[/Quote]

朋友,您这个语句一看就不对。谢谢您,我们不按人数来统计。我们是取分值

快溜 2012-10-26
  • 打赏
  • 举报
回复
2000用函数

create function f_name(@id int)
returns varchar(100)
as
begin
declare @str varchar(100)
set @str='';
select @str=@str+rtrim(sc) from tb
where id=@id order by sc desc
return @str
end

select id,sum(a.sc) from tb a
group by id
order by 2 desc,dbo.f_name(id) desc
visualcpu 2012-10-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

SQL code
create table tb(id int, sc int)
insert into tb
select 1,60 union
select 1,20 union
select 1,10 union
select 2,60 union
select 2,30 union
select 3,30

select id,sum(a.……
[/Quote]

您好,我这里报错。
开启时代 2012-10-26
  • 打赏
  • 举报
回复
select ID,SUM(学习总分) as 总分
from TB
group by id
order by SUM(学习总分) desc ,MAX(学习总分) desc,COUNT(*)
快溜 2012-10-26
  • 打赏
  • 举报
回复
  create table tb(id int, sc int)
insert into tb
select 1,60 union
select 1,20 union
select 1,10 union
select 2,60 union
select 2,30 union
select 3,30

select id,sum(a.sc) from tb a
group by id
order by 2 desc,(select sc+'' from tb where id=a.id
order by sc desc for xml path('')) desc

/*
id
----------- -----------
2 90
1 90
3 30

(3 row(s) affected)
visualcpu 2012-10-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

SQL code

----------------------------------------------------------------
-- Author :TravyLee(物是人非事事休,欲语泪先流!)
-- Date :2012-10-26 10:46:26
-- Version:
-- Microsoft SQL Server 2008 (RTM) - ……
[/Quote]


谢谢,您这个语句不用试,是没有用的。如果我部门3就一个人,但得了100分,部门3应该排第一。我们是先统计以部门为条件的总分。如果总分一样,取成员高分者所在部门排前的原则。
visualcpu 2012-10-26
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
部门ID,学习总分
1 60
1 20
1 10
2 60
2 30
3 30

要求,先部门总分高者排1名,如果部门之间的总分相同,取团队成员高分数者排前。

上面的实例数据 排名结果应该是 部门2排第一,部门1排第二 。因为部门1和部门2总分一样。但部门30分高于部……
[/Quote]

您好,谢谢您。我要的不是这样的排序,我要的统计排序结果

部门2 90
部门1 90
部门3 30
开启时代 2012-10-26
  • 打赏
  • 举报
回复
select ID
from TB
group by id
order by SUM(学习总分) desc ,COUNT(*)
  • 打赏
  • 举报
回复

----------------------------------------------------------------
-- Author :TravyLee(物是人非事事休,欲语泪先流!)
-- Date :2012-10-26 10:46:26
-- Version:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 6.1 <X86> (Build 7600: )
--
----------------------------------------------------------------
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
go
create table [test]([部门ID] int,[学习总分] int)
insert [test]
select 1,60 union all
select 1,20 union all
select 1,10 union all
select 2,60 union all
select 2,30 union all
select 3,30
go

with t
as(
select
*,
SUM([学习总分])over(partition by [部门ID]) as counts,
min([学习总分])over(partition by [部门ID]) as maxcount
from
test
)
select
[部门ID] ,
[学习总分]
from t order by counts desc,maxcount desc
----------------结果----------------------------
/*
部门ID 学习总分
----------- -----------
2 60
2 30
1 60
1 20
1 10
3 30

(6 行受影响)
*/

SQL77 2012-10-26
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 的回复:]
引用 27 楼 的回复:

drop table tb2
create table tb2(id int,Deptid int, sc int)
insert into tb2
select 1,1,90 union
select 2,1,20 union
select 3,1,10 union
select 4,2,50 union
select 5,2,30 union……
……
[/Quote]


drop table tb2
create table tb2(id int,Deptid int, sc int)
insert into tb2
select 1,1,50 UNION ALL
select 2,1,40 UNION ALL
select 3,1,20 UNION ALL
select 4,2,50 UNION ALL
select 5,2,25 UNION ALL
select 6,2,25 UNION ALL
select 7,3,50 UNION ALL
select 8,3,40 UNION ALL
select 9,2,40 UNION ALL
select 10,2,40 UNION ALL
select 11,3,40 UNION ALL
select 12,3,40 UNION ALL
select 13,3,10 UNION ALL
select 14,1,40 UNION ALL
select 14,1,30
go

alter function dbo.f_number(@id int)
returns varchar(4000)
as
begin
declare @len int
select @len=max(len(sc)) from tb2

declare @str varchar(4000)
select @str=isnull(@str+',','')+right(replicate('0',@len)+ltrim(sc),@len) from
tb2
where Deptid=@id order by sc desc
return @str
end
go
select Deptid,sum(a.sc) sumsc,dbo.f_number(Deptid) rn
from tb2 a
group by Deptid
order by 2 desc,dbo.f_number(Deptid) desc

/*3 180 50,40,40,40,10
1 180 50,40,40,30,20
2 180 50,40,40,25,25

哪里不行了?
visualcpu 2012-10-26
  • 打赏
  • 举报
回复
解决办法如下:


先定义一个函数。

create function D_Count(@paperId int,@deptid int )
returns varchar(4000)
as
begin
declare @str varchar(4000)
set @str=''
select @str=@str+rtrim(a.GetCountFen)+',' from ExamUser a,UserInfo b
where a.UserCode=b.UserCode and a.PaperID=@paperId and b.deptID=@deptid order by a.GetCountFen desc
return @str
end


第二步,在sql语句后面调用即可(注意看最后一个排序字段是函数)

select sum(GetCountFen) as GetCountFen,'0' as CountTime,'0' as ExamTime,b.DeptName,a.ExamName,a.PaperID,c.DeptID from ExamUser a,DeptInfo b,UserInfo c,PaperInfo d where a.UserCode=c.UserCode and b.ID=c.DeptID and d.ID=a.PaperID and a.PaperID=1

group by b.DeptName,c.DeptID,a.ExamName,a.PaperID order by sum(GetCountFen) desc,dbo.D_Count(a.PaperID,c.DeptID) desc
汤姆克鲁斯 2012-10-26
  • 打赏
  • 举报
回复
[Quote=引用 29 楼 的回复:]

继续完善中


SQL code


--> 测试数据:#tb
IF OBJECT_id('TEMPDB.DBO.#tb') IS NOT NULL DROP TABLE #tb
GO
CREATE TABLE #tb([id] int IDENTITY,[Deptid] INT,[score] INT)
INSERT #tb
SELECT 1,50 UNION ALL
SELECT ……
[/Quote]
最后少个desc


ORDER BY a.[SUMscore] DESC,a.[MAXscore] DESC ,b.[MAXscore]
汤姆克鲁斯 2012-10-26
  • 打赏
  • 举报
回复
继续完善中




--> 测试数据:#tb
IF OBJECT_id('TEMPDB.DBO.#tb') IS NOT NULL DROP TABLE #tb
GO
CREATE TABLE #tb([id] int IDENTITY,[Deptid] INT,[score] INT)
INSERT #tb
SELECT 1,50 UNION ALL
SELECT 1,40 UNION ALL
SELECT 1,40 UNION ALL
SELECT 1,30 UNION ALL
SELECT 1,20 UNION ALL
SELECT 2,50 UNION ALL
SELECT 2,40 UNION ALL
SELECT 2,40 UNION ALL
SELECT 2,25 UNION ALL
SELECT 2,25 UNION ALL
SELECT 3,50 UNION ALL
SELECT 3,40 UNION ALL
SELECT 3,40 UNION ALL
SELECT 3,40 UNION ALL
SELECT 3,10 UNION ALL
SELECT 4,170 UNION ALL
SELECT 4,10
GO
--SELECT * FROM #tb ORDER BY [Deptid] ,[score] DESC
IF OBJECT_id('TEMPDB.DBO.#t') IS NOT NULL DROP TABLE #t
GO
SELECT Deptid,[score],cnt=(SELECT cnt=COUNT(*)+1 FROM #tb WHERE [Deptid]=t.[Deptid] AND ([score]>t.[score] OR([score]=t.[score] AND id<t.id)))
INTO #t
FROM #tb AS t

SELECT a.Deptid,a.[SUMscore] FROM
(
SELECT Deptid,SUM([score]) AS [SUMscore],MAX([score]) AS [MAXscore]
FROM #tb AS t GROUP BY Deptid
)AS a
LEFT JOIN
(
SELECT Deptid,MAX([score]) AS [MAXscore] FROM #t AS t
WHERE EXISTS(SELECT 1 FROM #t WHERE [Deptid]<>t.[Deptid] AND [score]>t.[score]
AND NOT EXISTS (SELECT 1 FROM #t WHERE [Deptid]<>t.[Deptid] AND [score]=t.[score] AND [cnt]=t.[cnt]))
GROUP BY [Deptid]
)AS b
ON a.Deptid=b.Deptid
ORDER BY a.[SUMscore] DESC,a.[MAXscore] DESC ,b.[MAXscore]

----------------结果----------------------------
/*
Deptid SUMscore
4 180
2 180
1 180
3 180
*/
汤姆克鲁斯 2012-10-26
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 的回复:]

drop table tb2
create table tb2(id int,Deptid int, sc int)
insert into tb2
select 1,1,90 union
select 2,1,20 union
select 3,1,10 union
select 4,2,50 union
select 5,2,30 union……
[/Quote]

这个不行,你试试



drop table tb2
create table tb2(id int,Deptid int, sc int)
insert into tb2
select 1,1,50 UNION ALL
select 2,1,40 UNION ALL
select 3,1,20 UNION ALL
select 4,2,50 UNION ALL
select 5,2,25 UNION ALL
select 6,2,25 UNION ALL
select 7,3,50 UNION ALL
select 8,3,40 UNION ALL
select 9,2,40 UNION ALL
select 10,2,40 UNION ALL
select 11,3,40 UNION ALL
select 12,3,40 UNION ALL
select 13,3,10 UNION ALL
select 14,1,40 UNION ALL
select 14,1,30
go
visualcpu 2012-10-26
  • 打赏
  • 举报
回复
drop table tb2
create table tb2(id int,Deptid int, sc int)
insert into tb2
select 1,1,90 union
select 2,1,20 union
select 3,1,10 union
select 4,2,50 union
select 5,2,30 union
select 6,2,20 union
select 7,3,50 union
select 8,3,50
go




select sum(sc) as Sc,DeptID
,isnull((select sc from (select *,(select count(*) from tb2 a where a.deptid=tb2.deptid and (a.sc>tb2.sc or (a.sc=tb2.sc and a.id<tb2.id)))sort from tb2 )ttt where ttt.deptid=aa.deptid and sort=1),0)s1
,isnull((select sc from (select *,(select count(*) from tb2 a where a.deptid=tb2.deptid and (a.sc>tb2.sc or (a.sc=tb2.sc and a.id<tb2.id)))sort from tb2 )ttt where ttt.deptid=aa.deptid and sort=2),0)s2
from tb2 aa group by deptid order by sum(sc) desc,s1 desc,s2 desc

我一个同事写的。
SQL77 2012-10-26
  • 打赏
  • 举报
回复
IF OBJECT_ID('tb2') IS NOT NULL DROP TABLE tb2
GO
CREATE TABLE tb2(Deptid INT,sc INT)

insert into tb2
select 1,90 union all
select 1,20 union all
select 1,10 union all
select 2,50 union all
select 2,30 union all
select 2,20 union all
select 3,50 union all
select 3,50

go
alter function dbo.f_number(@id int)
returns varchar(4000)
as
begin
declare @len int
select @len=max(len(sc)) from tb2

declare @str varchar(4000)
select @str=isnull(@str+',','')+right(replicate('0',@len)+ltrim(sc),@len) from tb2
where Deptid=@id order by sc desc
return @str
end
go
select Deptid,sum(a.sc) sumsc,dbo.f_number(Deptid) rn
from tb2 a
group by Deptid
order by 2 desc,dbo.f_number(Deptid) desc

/*Deptid sumsc rn
----------- ----------- ------------
1 120 90,20,10
3 100 50,50
2 100 50,30,20


按这种方式来吧。不用考虑其他的东西。一定OK的。
按分的大小连接的字符串合计。并且每个长度都一样的。
xb12369 2012-10-26
  • 打赏
  • 举报
回复
我是来打酱油的,楼上已经有人做出来了,还问?

不结贴,难道是***

元方,你怎么看?
加载更多回复(12)

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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