⑴ 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 分組後怎麼把結果合並到一個類別下啊
--如果商品和類別在同一個表
--建表
CreateTableT
(
商品Varchar(10),
類別Varchar(10),
數量Int
)
--插入數據
InsertIntoTValues('蘋果','水果',10)
InsertIntoTValues('葡萄','水果',20)
InsertIntoTValues('西紅柿','蔬菜',30)
--方法一,按商品類別分類求和,加withcube選項
SelectISNULL(商品,'分類-'+類別)As商品,數量
From(
Select商品,類別,SUM(數量)As數量FromT
GroupBy商品,類別
WithCube
)S
Where類別IsNotNull
--方法二,按商品和類別分別匯總後合拼在一起,然後排序
Select商品,數量
From(
Select商品,Max(類別)As類別,SUM(數量)As數量FromT
GroupBy商品
UnionAll
Select'分類-'+類別,類別+'Z',SUM(數量)As數量FromT
GroupBy類別
)S
OrderBy類別
--如果沒有類別,需要先建一個對照表
--建表
CreateTableM
(
商品Varchar(10),
類別Varchar(10)
)
CreateTableN
(
商品Varchar(10),
數量Int
)
--插入數據
InsertIntoMValues('蘋果','水果')
InsertIntoMValues('葡萄','水果')
InsertIntoMValues('西紅柿','蔬菜')
InsertIntoNValues('蘋果',10)
InsertIntoNValues('葡萄',20)
InsertIntoNValues('西紅柿',30)
InsertIntoNValues('蘋果',15)
--查詢
SelectIsNull(商品,'分類-'+類別)As商品,數量
From(
Selectn.商品,類別,SUM(數量)As數量FromnLeftJoinm
onm.商品=n.商品
GroupByn.商品,類別
WithCube
)s
Where類別isnotnull
⑷ sql 分組和排序
如果是SQLServer 2005以上可以這樣寫
select 學生ID、課程編號、成績 from
(select 學生ID、課程編號、成績 ,row_number(partition by 課程編號 order by 課程編號,成績)as rn from student) as T where rn <=2 order by 課程編號、成績
⑸ sql分組排序
group by語句必須和聚合函數一起使用.
select c,max(d) from a group by c order by max(d) desc
這樣子可以.
因為一條SELECT語句只可以返回一個結果集...
此句返回按c分組後並按每組中最大的d值進行排序.
⑹ SQL如何分類後順序連接字串
----以下在sql2005測試通過。
create table table1(bm varchar(10),ry varchar(10),mas varchar(10))
insert table1
select 'org1','sta1','mas' union all
select 'org1','sta2','' union all
select 'org1','sta3','' union all
select 'org2','sta4','mas' union all
select 'org2','sta5',''
---以前就回答過這樣的問題了,給你2個參考方案:
--實現方式1、利用函數
--分析,可以按照ID進行分組,然後對每個ID的value進行函數合並。
if object_id(N'dbo.f_str',N'FN') is not null
drop function dbo.f_str
go
CREATE FUNCTION dbo.f_str(@bm varchar(10))
RETURNS varchar(1000)
AS
BEGIN
DECLARE @r varchar(1000)
SELECT @r = isnull(@r+';','')+ry FROM table1 WHERE bm=@bm
RETURN @r
END
GO
-- 調用函數
select * from(select bm,mas from table1 where (mas<>'' and not mas is null))t1
outer apply
(
SELECt [ry] = dbo.f_str(bm) FROM table1 where t1.bm=bm group by bm
)t2
--實現方式2、利用group by和for XML
--利用for XML構造每個ID的vlaue合並,然後和ID列進行合並
--這里只是用了group by進行分組
select * from(select bm,mas from table1 where (mas<>'' and not mas is null))t1
outer apply
(select [ry]=stuff((select ';'+ry from table1 where bm=t2.bm for xml path('')), 1, 1, '')
from table1 t2 where t1.bm=bm
group by bm
)t3
--樓主滿意了沒。
⑺ SQL分組匯總排序
華子,我來看你.....
⑻ sql 分組排序
沒有計算機理解的順序,必需構造一個表示順序的函數。完整的實現語句如下:
select *,
case check_id
when 4 then 1
when 2 then 2
else check_id*10
end as def_order
from 表 order by def_order,idate
這里用CASE函數定義一個新的順序,如果check_id=4,新的排序號為1;如果check_id=2,新的排序號為2;如果check_id為其它,新的排序號為check_id*10,這樣能滿足要求的順序。
樓上的兄弟的解決方案,從理論上不能保證滿足要求,因為查詢輸出記錄的順序不一定是記錄輸入表的順序。
⑼ sql按順序分成三組
sql分組使用groupby語法。
groupby是sql中比較強大的功能,是在對數據分組統計時必不可少的用法。但是,對於很多經驗不足的同學,經常會寫錯。
⑽ 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