search
HomeDatabaseMysql Tutorialoracle 统计分析函数之lead和lag

oracle 统计分析函数之lead和lag

Jun 07, 2016 pm 03:31 PM
oraclefunctionAdd toStatistical Analysis

无需添加内联结构便可以是实现访问当前行附近行的字段的lead :向后 lag: 向前. 语法结构: lag(value_expr [,offset][,default]) over([query_partition_clause] order by Order_by_clause) lead(value_expr [,offset][,default]) over([query_partition_cla

无需添加内联结构便可以是实现访问当前行附近行的字段的值lead :向后 lag: 向前.

 语法结构:

      lag(value_expr [,offset][,default]) over([query_partition_clause] order by Order_by_clause)

     lead(value_expr [,offset][,default]) over([query_partition_clause] order by Order_by_clause)

    参数说明:

     value_expr值表达式,通常是字段,也可是是表达式。value_expr本身不支持分析函数,也就是lead不支持多层调用。

     offset偏移,应该是很熟悉的数学概念了,或者是相对偏移,表格来开当前行的第offset行,如果offset是整数就表示是顺序下的前第n行,如果是负数就是往后第n行。如果不提供这个参数,就是默认为1.

      default默认值,如果没有找到,应该返回什么值的意思,有点类似nvl(col,value)。如果没有设置,且找不到,那么就返回Null

      over 可以简单地翻译为在什么。。。的基础之上

      query_partition_clause 分区语句,对结果集合分区的语句,是可选的,如果没有就是所有的一个分区。

      Order_by_clause排序语句必须需要 ,形如order by xxx desc/asc

 

    解释示例:

     SQL> select * from test_value;

      

              MONS JJR               CJL       CJJE

        ---------- ---------- ---------- ----------

           200801 LZF               250       1999

            200802 LZF               200       2000

            200803 LZF               300       1000

            200804 LZF                23        189

            200805 LZF               356        456

           200806 LZF               100        200

            200807 LZF               600        700

            200808 LZF                23        123

            200809 LZF               400        500

      

        9 rows selected

      

        SQL>

        SQL> select rownum序号,Mons,cjl cjl_01,

          2  lead(cjl,1) over (order by mons desc) cjl_02,

          3  lead(cjl,2) over (order by mons desc) cjl_03,

          4  lead(cjl,3) over (order by mons desc) cjl_04,

          5  lead(cjl,4) over (order by mons desc) cjl_05,

          6  lead(cjl,5) over (order by mons desc) cjl_06,

          7  lead(cjl,6) over (order by mons desc) cjl_07,

          8  lead(cjl,7) over (order by mons desc) cjl_08,

          9  lead(cjl,8) over (order by mons desc) cjl_09

         10   from test_value

             序号       MONS     CJL_01     CJL_02     CJL_03     CJL_04     CJL_05     CJL_06     CJL_07     CJL_08     CJL_09

        ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------

                 9     200809        400         23        600        100        356         23        300        200        250

                 8     200808         23        600        100        356        23        300        200        250

                 7     200807        600        100        356         23        300        200        250          

                 6     200806        100        356         23        300        200        250                    

                 5     200805        356         23        300        200        250                                

                 4     200804         23        300        200        250                                           

                 3     200803        300        200        250                                                      

                 2     200802        200        250                                                                 

                 1    200801        250                  

 实践使用示例:         

   select id,lead(id,1)over(partition by  call_req_id order by call_req_id ,id),type,analyst,time_stamp from act_log order by call_req_id ,id

 

扩展:

求字段差值为固定值的所有记录集合,一般会考虑取余mod(field,difNumber);

例如: 求表中所有ID相差为7的所有记录,

可以考虑 (每行记录的id - 第一行记录的id)对7取余等于0;

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL BLOB : are there any limits?MySQL BLOB : are there any limits?May 08, 2025 am 12:22 AM

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

MySQL : What are the best tools to automate users creation?MySQL : What are the best tools to automate users creation?May 08, 2025 am 12:22 AM

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

MySQL: Can I search inside a blob?MySQL: Can I search inside a blob?May 08, 2025 am 12:20 AM

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc

MySQL String Data Types: A Comprehensive GuideMySQL String Data Types: A Comprehensive GuideMay 08, 2025 am 12:14 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,idealforconsistentlengthdatalikecountrycodes;2)VARCHARforvariable-lengthstrings,suitableforfieldslikenames;3)TEXTtypesforlargertext,goodforblogpostsbutcanimpactperformance;4)BINARYandVARB

Mastering MySQL BLOBs: A Step-by-Step TutorialMastering MySQL BLOBs: A Step-by-Step TutorialMay 08, 2025 am 12:01 AM

TomasterMySQLBLOBs,followthesesteps:1)ChoosetheappropriateBLOBtype(TINYBLOB,BLOB,MEDIUMBLOB,LONGBLOB)basedondatasize.2)InsertdatausingLOAD_FILEforefficiency.3)Storefilereferencesinsteadoffilestoimproveperformance.4)UseDUMPFILEtoretrieveandsaveBLOBsco

BLOB Data Type in MySQL: A Detailed Overview for DevelopersBLOB Data Type in MySQL: A Detailed Overview for DevelopersMay 07, 2025 pm 05:41 PM

BlobdatatypesinmysqlareusedforvoringLargebinarydatalikeImagesoraudio.1) Useblobtypes (tinyblobtolongblob) Basedondatasizeneeds. 2) Storeblobsin Perplate Petooptimize Performance.3) ConsidersxterNal Storage Forel Blob Romana DatabasesizerIndimprovebackupupe

How to Add Users to MySQL from the Command LineHow to Add Users to MySQL from the Command LineMay 07, 2025 pm 05:01 PM

ToadduserstoMySQLfromthecommandline,loginasroot,thenuseCREATEUSER'username'@'host'IDENTIFIEDBY'password';tocreateanewuser.GrantpermissionswithGRANTALLPRIVILEGESONdatabase.*TO'username'@'host';anduseFLUSHPRIVILEGES;toapplychanges.Alwaysusestrongpasswo

What Are the Different String Data Types in MySQL? A Detailed OverviewWhat Are the Different String Data Types in MySQL? A Detailed OverviewMay 07, 2025 pm 03:33 PM

MySQLofferseightstringdatatypes:CHAR,VARCHAR,BINARY,VARBINARY,BLOB,TEXT,ENUM,andSET.1)CHARisfixed-length,idealforconsistentdatalikecountrycodes.2)VARCHARisvariable-length,efficientforvaryingdatalikenames.3)BINARYandVARBINARYstorebinarydata,similartoC

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool