⑴ 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相同表分組排列組合的問題
建兩個序列,並通過一個函數調用序列(union 不支持直接使用序列),用於排序。
先用第二組所有行(3行),union all 3遍第一組第一行(left join 第二組,行數就和第二組一樣了)
在用第二組所有行(3行),union all 3遍第一組第二行(left join 第二組,行數就和第二組一樣了)
最後按照序號,組別,編號排序
/*
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)這個列的別名