当前位置:首页 » 编程语言 » sql分组得到排序
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql分组得到排序

发布时间: 2022-08-16 16:31:58

‘壹’ sql先分组后排序的问题

selectregtime,ccount,row_number()over(partitionbycasewhenccount=0then0else1endorderbyregtime)from表

‘贰’ SQL如何对分组后的结果进行排序并且取前几名

SQL取分组中的前几名

[sql] www.2cto.com
create table #aa(class varchar(10),name varchar(10),lang int,math int)
go

insert into #aa
select '1','a1',60,70
union all
select '1','a2',80,70
union all
select '1','a3',90,70
union all
select '1','a4',50,70
go

insert into #aa
select '2','b1',60,70
union all
select '2','b2',90,90
union all
select '2','b3',90,70
union all
select '2','b4',50,70

go

select * from #aa

--取每年级前一名的成级
select * from
(select ROW_NUMBER() over(partition by class order by lang+math desc) rr, * from #aa ) a
where rr<2
--取每年级前二名的成级
select * from
(select ROW_NUMBER() over(partition by class order by lang+math desc) rr, * from #aa ) a
where rr<3

‘叁’ sql 分组排序

with
ranked
as
(select
t1.*,
rank()
over
(partition
by
name
order
by
price
desc)
as
rk
from
table1
t1
join
table2
t2
on
t1.goods=t2.goods)
select
a.name,
b.goods
as
goods1,
c.goods
as
goods2,
d.goods
as
goods3
from
(select
name
from
table1
group
by
name)
a
join
(select
name,
goods
from
ranked
where
rk
=1)
b
on
b.name=a.name
join
(select
name,
goods
from
ranked
where
rk
=2)
c
on
c.name=a.name
join
(select
name,
goods
from
ranked
where
rk
=3)
d
on
d.name=a.name
order
by
name

‘肆’ 在SQL语句中,分组用什么子句,排序用什么子句

排序采用ORDER BY语句:ORDER BY 语句用于根据指定的列对结果集进行排序。ORDER BY 语句默认按照升序对记录行排序。如果希望按照降序对记录进行排序,可以使用 DESC关键字。

SQL有两种使用方式:

一是联机交互使用,这种方式下的SQL实际上是作为自含型语言使用的。

另一种方式是嵌入到某种高级程序设计语言(如C语言等)中去使用。前一种方式适合于非计算机专业人员使用,后一种方式适合于专业计算机人员使用。尽管使用方式不向,但所用语言的语法结构基本上是一致的。

(4)sql分组得到排序扩展阅读:

以同一种语法结构提供两种使用方式:

SQL既是自含式语言,又是嵌入式语言。作为自含式语言,它能够独立地用于联机交互的使用方式,用户可以在终端键盘上直接输入SQL命令对数据库进行操作。作为嵌入式语言,SQL语句能够嵌入到高级语言(如C、 C#、JAVA)程序中,供程序员设计程序时使用。

而在两种不同的使用方式下,SQL的语法结构基本上是一致的。这种以统一的语法结构提供两种不同的操作方式,为用户提供了极大的灵活性与方便性。

‘伍’ SQL分组排序

create
table
#a
(a
char(10),b
int)
insert
#a
select
'a',1
insert
#a
select
'a',5
insert
#a
select
'b',1
insert
#a
select
'b',4
insert
#a
select
'b',3
insert
#a
select
'b',5
create
table
#c
(id
int
identity(1,1),
a
varchar(10))
insert
into
#c
select
distinct
a
from
#A
create
table
#d
(id
int
,
a
varchar(10),b
int)
declare
@a
int
declare
@b
int
select
@a=min(id)
from
#c
select
@b=max(id)
from
#c
while
(@a<=@b)
begin
select
identity(int,1,1)
as
id
,
t1.a,t1.b
into
#b
from
#a
t1,
#c
t2
where
t1.a=t2.a
and
t2.[id]=@a
order
by
t1.b
insert
into
#d
select
*
from
#b
drop
table
#b
set
@a=@a+1
end
select
*
from
#d

‘陆’ sql 分组后排序

select*,ROW_NUMBER()over(partitionby品牌orderby市场份额desc)as排名FROM市场份额表

‘柒’ SQL 分组统计并排序

group
by语句必须和聚合函数一起使用.
select
c,max(d)
from
a
group
by
c
order
by
max(d)
desc
这样子可以.
因为一条select语句只可以返回一个结果集...
此句返回按c分组后并按每组中最大的d值进行排序.

‘捌’ SQL如何对分组后运算出来的结果进行排序

提供两种方案,第一种是降序
select sno,avg(grade) 'nihao' from sc
group by sno order by 'nihao' desc
第二种是升序
select sno,avg(grade) 'nihao' from sc
group by sno order by 'nihao' asc

其中‘nihao’表示avg(grade)这个列的别名

‘玖’ sql 如何分组排序同时进行

1、首先输入代码:

SELECT * FROM (select * from CJ where Gender='女') m

where( select COUNT(*) from (select * from CJ where Gender='女') n

where m.Classid = n.Classid and n.English > m.English)<2

order by Classid, English desc

2、然后再输入代码:

SELECT * FROM CJ m

where(

select COUNT(*) from CJ n

where m.Classid = n.Classid and n.English > m.English and n.Gender='女')<2 --指的是内表

and Gender='女' --指的是外表

order by Classid, English desc