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

sql分组排序并列值

发布时间: 2023-02-19 07:30:38

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 如何分组排序同时进行

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

⑶ 关于SQL相同表分组排列组合的问题

  1. 建两个序列,并通过一个函数调用序列(union 不支持直接使用序列),用于排序。

  2. 先用第二组所有行(3行),union all 3遍第一组第一行(left join 第二组,行数就和第二组一样了)

  3. 在用第二组所有行(3行),union all 3遍第一组第二行(left join 第二组,行数就和第二组一样了)

  4. 最后按照序号,组别,编号排序

/*
dropsequenceBig_Letter1;
CreatesequenceBig_Letter1
Incrementby1
Startwith65
Maxvalue999999
Minvalue1
Nocycle
nocache;

dropsequenceBig_Letter2;
CreatesequenceBig_Letter2
Incrementby1
Startwith65
Maxvalue999999
Minvalue1
Nocycle
nocache;

--获取数列下一个值
createorreplacefunctionget_seq_next(seq_nameinvarchar2)returnnumber
is
seq_valnumber;
begin
executeimmediate'select'||seq_name||'.nextvalfromal'intoseq_val;
returnseq_val;
endget_seq_next;

*/

withtmpas(
select'1'groupid,'1'numfromal
union
select'1','2'fromal
union
select'2','1'fromal
union
select'2','2'fromal
union
select'2','3'fromal
)
selectchr(get_seq_next('Big_Letter1'))xuhao,t1.groupid,t1.num
fromtmpt1wheregroupid='2'
unionall
selectchr(get_seq_next('Big_Letter2'))xuhao,t1.groupid,t1.num
fromtmpt1,tmpt2
wheret1.groupid='1'andt1.num='1'andt2.groupid='2'
union
selectchr(get_seq_next('Big_Letter1'))xuhao,t1.groupid,t1.num
fromtmpt1wheregroupid='2'
unionall
selectchr(get_seq_next('Big_Letter2'))xuhao,t1.groupid,t1.num
fromtmpt1,tmpt2
wheret1.groupid='1'andt1.num='2'andt2.groupid='2'
orderbyxuhao,groupid,num

执行结果如下:

按照这个思路拼动态SQL吧

⑷ sql 分组后排序

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

⑸ sql语句 按一列分组 然后再按别一列组内排序

sql 按 group 单纯的对unitname 分组查询后 是 "统计数据" ,不存在组内情况,并不会带有 voucherID,不能排序,对 voucherID 的排序也是无意义。

或者说你按 unitname、voucherID 俩个分组,然后 按voucherID 排序,这个是可以实现的。

⑹ SQL 分组统计并排序

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

⑺ sql 语句 并列排名的问题

无论sqlserver 还是 oracle


rank() 或者 dens_rank() 函数。

select name,score,rank() over(order by score) tt from t;
如果有并列的话 . 他们后边的排序会变成这样的。 根据你的需求选用。
1
2
2
4

select name,score, dens_rank() over(order by score) tt from t;
1
2
2
3

感觉你用地2个,, dense_rank() 然后设置。 让 虚拟的这个排序列tt<=5

⑻ SQL 分组统计并排序

with tmp as
(select row_number() over(order by count(val)) as rn,val,count(val) as cnum from table1 group by val)
select * from tmp where rn<3
要想区别并列现象还要复杂一些,可能简单的语句解决不了

⑼ sql语句 在分组内排序

只能用存储过程
思路如下
将分组那个字段比如是A
Create temp table tmp_b
foreach
select distinct A into bianlianga from table
insert into tmp_b
select top 2 B from table where A=bianlianga order by B ;
delete from table where A=bianlianga and B in(select * from tmp_b);
end foreach

⑽ 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)这个列的别名