分組彙總 with rollup和with cube和compute by 和排名函數

retmxls-- 商品銷售明細表express

rq--日期ssh

spid--商品信息函數

sshje--每筆銷售記錄的金額spa

select spid,rq, sum(sshje) from retmxls group by rq,spid with ROLLUP order by spid排序

根據group by 後的第一個字段進行分組,我這裏的是rq,那就是同日期的爲一組,並在這一組完的最後一行插入一個空行,顯示這個組的sshje 的彙總ci


select spid,rq, sum(sshje) from retmxls group by spid,rq with cube order by spidit

根據group by 後的每個字段進行分組,並在這一組完的最後一行插入一個空行,顯示這個組的sshje 的彙總 io

下圖的結果中有2026.6這個是2005-05-01這個日期的全部商品的彙總額,而4702.58是整個表的全部商品所table

有日期的總彙總,而206.66是SPH10000002在全部天的彙總。select


select spid,rq,sshje from retmxls order by rq compute sum(sshje)

對compute 後的sum字段進行彙總,返回明細和一個彙總兩個結果

 

select spid,rq,sshje from retmxls order by rq compute sum(sshje) by (rq)

 對compute 後的sum字段進行彙總,對by後的字段進行分組,返回由日期分組後的每一個明細和每一個彙總的多個結果

 

 

 

 

下面再說幾個比較有用的排名函數(RANK ()NTILE (integer_expression)、row_number() 、DENSE_RANK)

一、RANK ()

語法

RANK ( )    OVER ( [ < partition_by_clause > ] < order_by_clause > )

參數

< partition_by_clause>

將 FROM 子句生成的結果集劃分紅 RANK 函數適用的分區。若要了解 PARTITION BY 語法,請參閱 OVER 子句 (Transact-SQL)。

< order_by_clause>

肯定將 RANK 值應用於分區中的行時所基於的順序。有關詳細信息,請參閱 ORDER BY 子句 (Transact-SQL)。當在排名函數中使用 <order_by_clause> 時,不能用整數表示列。

示列:select rank () over(PARTITION BY sshje order by spid) as r,spid,sshje from retmxls結果生成以sshje分區,按spid排序的序號,返回結果同一spid序號同樣,下一序號從實際行號開始。而不是上一序號加1

也能夠select rank () over(order by spid) as r,spid,sshje from retmxls

返回結果同一spid序號同樣,下一序號從實際行號開始。而不是上一序號加1

 

二、NTILE (integer_expression)

語法

NTILE (integer_expression)    OVER ( [ <partition_by_clause> ] < order_by_clause > )

參數

integer_expression

一個正整數常量表達式,用於指定每一個分區必須被劃分紅的組數。

integer_expression 的類型能夠爲 intbigint

 

<partition_by_clause>

將 FROM 子句生成的結果集劃分紅 RANK 函數適用的分區。

< order_by_clause>

肯定 NTILE 值分配到分區中各行的順序。有關詳細信息,請參閱 ORDER BY 子句 (Transact-SQL)。當在排名函數中使用 <order_by_clause> 時,不能用整數表示列。

示列:select NTILE (4) over(PARTITION BY sshje order by spid) as r,spid,sshje from retmxls

根據sshje進行分區,按照spid進行排序,把每一個分區分紅4份

也能夠select NTILE (4) over(order by spid) as r,spid,sshje from retmxls

將整個結果分紅四份

三、DENSE_RANK ( )  

語法

 

 

DENSE_RANK ( )    OVER ( [ < partition_by_clause > ] < order_by_clause > )

參數

< partition_by_clause>

將 FROM 子句生成的結果集劃分爲數個應用 DENSE_RANK 函數的分區。若要了解 PARTITION BY 語法,請參閱 OVER 子句 (Transact-SQL)。

< order_by_clause>

肯定將 DENSE_RANK 值應用於分區中各行的順序。整數不能表示排名函數中使用的 <order_by_clause> 中的列。

示列:select DENSE_RANK() over( order by spid) as r,spid,sshje from retmxls

返回結果同NTILE的區別是他的下一區的序號是上一區的數值加1而不是實際的行數。

select DENSE_RANK() over(PARTITION BY sshje order by spid) as r,spid,sshje from retmxls

生成結果根據sshje分區,spid排序,生成序號,序號是連續的順序

4row_number()

語法

 

 

ROW_NUMBER ( )     OVER ( [ <partition_by_clause> ] <order_by_clause> )

參數

<partition_by_clause>

將 FROM 子句生成的結果集劃入應用了 ROW_NUMBER 函數的分區。若要了解 PARTITION BY 語法,請參閱 OVER 子句 (Transact-SQL)。

<order_by_clause>

肯定將 ROW_NUMBER 值分配給分區中的行的順序。有關詳細信息,請參閱 ORDER BY 子句 (Transact-SQL)。當在排名函數中使用 <order_by_clause> 時,不能用整數表示列。

示列:select row_number() over(order by spid) as r,spid,sshje from retmxls

生成結果,以spid排序,生成連續的序號。和標識種子相似

select row_number() over(PARTITION BY sshje order by spid) as r,spid,sshje from retmxls

生成以sshje分區的序號,每一個分區從1開始

相關文章
相關標籤/搜索