偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

數(shù)據(jù)分析師的SQL功底該學(xué)到什么程度?

運(yùn)維 數(shù)據(jù)庫(kù)運(yùn)維 數(shù)據(jù)分析
常有朋友問,數(shù)據(jù)分析師的SQL功底該學(xué)到什么程度。今天就先談?wù)?T-SQL 中的 Window Function.

[[416110]]

本文轉(zhuǎn)載自微信公眾號(hào)「有關(guān)SQL」,作者Lenis。轉(zhuǎn)載本文請(qǐng)聯(lián)系有關(guān)SQL公眾號(hào)。

常有朋友問,數(shù)據(jù)分析師的SQL功底該學(xué)到什么程度。今天就先談?wù)?T-SQL 中的 Window Function.

Window Function 包含了 4 個(gè)大類。分別是:

  • 1 - Rank Function
  • 2 - Aggregate Function
  • 3 - Offset Function
  • 4 - Distribution Function.

1 - Rank Function 平常用到最多

  • 1.1 Rank() Over()
  • 1.2 Row_Number() Over()
  • 1.3 Dense_Rank() Over()
  • 1.4 NTILE(N) Over()

這四個(gè)函數(shù),要注意的地方有兩點(diǎn):

a. Rank() Over() 與 Row_Number() Over() :

兩者唯一的區(qū)別,就在于Row_Number() Over() 真正實(shí)現(xiàn)了相同條件的兩條或者多條記錄是用唯一值來(lái)區(qū)別的

b. Rank() Over() 與 Dense_Rank() Over() :

這兩者的區(qū)別,在于他們對(duì)位于相同排名之后的名次,是接著相同排名的連續(xù)數(shù)(Rank) 還是相隔 N 個(gè)相同記錄個(gè)數(shù)之后的連續(xù)數(shù)(Dense_Rank)。

所以 Rank 出來(lái)的結(jié)果都是連續(xù)數(shù)字,而 Dense_Rank 出來(lái)的結(jié)果有可能有跳格數(shù)。

c. 除了有用法上的區(qū)別外,順帶說說分頁(yè)的實(shí)現(xiàn):

第一種,我們平常用 Row_Number() 加 Top (N) 來(lái)實(shí)現(xiàn) :

  1. select top(100) * 
  2.  
  3.      from ( select  
  4.  
  5.                          OrderId 
  6.  
  7.                      ,    OrderMonth 
  8.  
  9.                       ,    OrderAmount 
  10.  
  11.                       ,    Row_Number() Over(                                    
  12.  
  13.                                  OrderBy OrderAmount DESC
  14.  
  15.                              AS Amt_Order 
  16.  
  17.                   from FctSales) tmp 
  18.  
  19.       Where Amt_Order between 2000 and 3000 

第二種,SQL Server 2012 之后的新功能:

  1. Select    OrderId 
  2.  
  3.             ,    OrderMonth 
  4.  
  5.             ,    OrderAmount 
  6.  
  7.    From FctSales 
  8.  
  9.   Order by OrderAmount Desc 
  10.  
  11.  
  12.  
  13.   OffSet 2000 ROWS 
  14.  
  15.   Fetch Next 100 ROWS Only  

按照量的大小倒序排,取第 2000 條后的記錄中前 100 條。

2 - Aggregate Function. 聚合數(shù)據(jù)

  • 2.1 - Sum() Over()
  • 2.2 - Count() Over()
  • 2.3 - AVG() Over()
  • 2.4 - MIN() Over()
  • 2.5 - MAX() Over()

在使用 Aggregation 函數(shù)的時(shí)候,唯一要注意的地方就是 Order 子句。

  1. function_name(<arguments>) Over( 
  2.  
  3.     [ <window partition clause>] 
  4.  
  5.     [ <window Order clause> 
  6.  
  7.             [ <window frame clause>] 
  8.  
  9.     ]) 

Over::

  1. Over( 
  2.  
  3.             [    <PARTITION BY clause>    ] 
  4.  
  5.             [    <ORDER BY clause>          ] 
  6.  
  7.             [    <ROW or RANGE clause>  ] 
  8.  
  9.     ) 

::窗口中的窗口

  1. ROWS | RANGE 
  2.  
  3.     BETWEEN  
  4.  
  5.         UNBOUNDED PRECDEDING  | 
  6.  
  7.          <N>  PRECEDING     | 
  8.  
  9.           <N>  FOLLOWING   | 
  10.  
  11.             CURRENT ROW 
  12.  
  13.      AND 
  14.  
  15.             UNBOUNDED FOLLOWING  | 
  16.  
  17.              <N> PRECEDING  | 
  18.  
  19.              <N> FOLLOWING  | 
  20.  
  21.              CURRENT ROW 

舉一個(gè)例子:

  1. select custid 
  2.  
  3.    , ordermonth 
  4.  
  5.    , ordervolume 
  6.  
  7.     , sum(ordervolume)  
  8.  
  9.           over(    partition by custid 
  10.  
  11.                 order by ordermonth asc 
  12.  
  13.                 rows between     
  14.  
  15.                          unbounded preceding 
  16.  
  17.                 and    current row) 
  18.  
  19.         as cumulatedVolume 
  20.  
  21.  from FctSales 

統(tǒng)計(jì)了截止到目前為止,每一天的累計(jì)總量。

3 - Offset Function:定位記錄

  • 3.1 Lead()
  • 3.2 LAG()
  • 3.3 First_Value()
  • 3.4 Last_Value()
  • 3.5 Nth_Value()

這一類比較好理解,根據(jù)當(dāng)前的記錄,獲取前后 N 條數(shù)據(jù)。

4 - Distribution Function: 分布函數(shù)

  • 4.1- PERCENT_RANK()
  • 4.2 - CUME_DIST()
  • 4.3 - PERCENT_COUNT()-
  • 4.4 - PERCENT_DISC()

 

這一類應(yīng)用,到目前為止,未用過。適用于財(cái)會(huì)類的統(tǒng)計(jì)。

 

責(zé)任編輯:武曉燕 來(lái)源: 有關(guān)SQL
相關(guān)推薦

2017-04-11 09:08:02

數(shù)據(jù)分析Python

2018-07-19 19:50:48

大數(shù)據(jù)數(shù)據(jù)分析師網(wǎng)站分析

2012-08-08 09:00:29

數(shù)據(jù)分析師

2015-08-18 13:26:05

數(shù)據(jù)分析

2024-07-26 21:36:43

2016-10-21 14:41:22

數(shù)據(jù)分析師大數(shù)據(jù)

2015-04-03 11:19:21

大數(shù)據(jù)大數(shù)據(jù)分析師

2013-07-29 15:58:28

大數(shù)據(jù)數(shù)據(jù)分析

2017-02-13 19:25:24

2019-09-16 11:37:07

大數(shù)據(jù)數(shù)據(jù)分析工具

2012-08-07 17:32:25

數(shù)據(jù)分析師

2015-08-17 09:39:40

大數(shù)據(jù)

2023-07-08 23:05:01

數(shù)據(jù)分析運(yùn)營(yíng)

2021-03-26 07:37:34

數(shù)據(jù)分析工具技能

2020-05-12 10:44:19

數(shù)據(jù)分析師薪資數(shù)據(jù)

2016-01-26 10:33:23

大數(shù)據(jù)分析工具數(shù)據(jù)分析師

2016-02-23 09:59:14

數(shù)據(jù)分析數(shù)據(jù)分析師數(shù)據(jù)管理

2011-05-30 09:55:14

數(shù)據(jù)分析師

2020-07-20 07:00:00

數(shù)據(jù)分析師數(shù)據(jù)分析大數(shù)據(jù)

2018-01-28 15:40:20

數(shù)據(jù)分析師職業(yè)規(guī)劃數(shù)據(jù)分析
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)