{"id":8795,"date":"2019-01-30T18:41:07","date_gmt":"2019-01-30T18:41:07","guid":{"rendered":"https:\/\/dbtut.com\/?p=8795"},"modified":"2019-01-30T18:42:05","modified_gmt":"2019-01-30T18:42:05","slug":"lead-and-lag-functions-in-sql-servertsql","status":"publish","type":"post","link":"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/","title":{"rendered":"LEAD and LAG Functions in SQL Server(TSQL)"},"content":{"rendered":"<p>As a consultant and training provider in business intelligence projects on SQL Server, some analytic functions I expected to be announced for a long time were announced with the SQL Server 2012 version. We will examine two of these 8 new analytic functions, which are announced with SQL Server 2012: <strong>LEAD and LAG<\/strong>.<\/p>\n<h3>LEAD Function in SQL Server(TSQL):<\/h3>\n<p>The lead function allows you to move forward from the current row within the targeted result set.\u00a0This action may be on the entire result set, or it may be on a specified part of the result set. In this way, we can compare the values in the table as we need without self-joining the table.<\/p>\n<p>Now let&#8217;s look at the basic template:<\/p>\n<pre class=\"lang:default decode:true\">LEAD ( scalar_expression [ ,offset ] , [ default ] )\n\u00a0\u00a0\u00a0\u00a0OVER ([\u00a0partition_by_clause]\u00a0order_by_clause)<\/pre>\n<p><strong>The lead function consists of two basic parts. <\/strong><\/p>\n<h4>First Part:<\/h4>\n<p>There is scalar expression, offset and default value if there is no data. There are a few things to be aware of at this point. The function written in (scalar_expression) must definitely return a single(scalar) value. Another issue with this section is that no other analytic function should be used in scalar expression.<\/p>\n<p>In this section, we will decide the column that will be sorted and whether or not to be grouped according to a column.<\/p>\n<p>Let&#8217;s make examples for a better understanding of LEAD Function.<\/p>\n<p>In our examples, we used the AdventureWorks database that was create for SQL Server 2008R2.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"lang:default decode:true \">USE AdventureWorks2008R2\nGO\nSELECT BusinessEntityID,YEAR(QuotaDate) AS SalesYear,Month(QuotaDate)\/3+1 AS SalesQuarter, \nSalesQuota AS CurrentQuota, LEAD(SalesQuota, 1,0) OVER (ORDER BY YEAR(QuotaDate)) AS NextQuota\nFROM Sales.SalesPersonQuotaHistory\nWHERE BusinessEntityID = 279 AND YEAR(QuotaDate) = 2006;<\/pre>\n<p id=\"iOOyUfn\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-8797  aligncenter\" src=\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/01\/img_5c51d6fb7ad35.png\" alt=\"\" width=\"771\" height=\"265\" \/><\/p>\n<p>When I run the above query, the sales quota data returned on a quarterly basis, as you saw in the result. With the LEAD function, we have added the next period sales quota data to each line. One important point here is that the NextQuota value returns 0 for the last quarter of 2006 because this data is a data of 2007. Although the data for the last quarter existed in the table, it was not listed in the result set.<\/p>\n<p>In our example, if you make a change in the Lead function as follows, you can see that the process is successful. This also explains why it is called as scalar_expression.<\/p>\n<pre class=\"lang:default decode:true\">LEAD(SalesQuota+10, 1,0) OVER (ORDER BY YEAR(QuotaDate))<\/pre>\n<h4>Second Part:<\/h4>\n<p><strong>OVER Clause in the lead function:<\/strong><\/p>\n<p>When we examine the query and the results, we see that the Order BY process in OVER Clause has been proceed before the Order By Clause at the end of the query and the LEAD function is processed according to this order. Then we see that Order By Clause at the end of the query is proceed.<\/p>\n<pre class=\"lang:default decode:true \">SELECT BusinessEntityID,YEAR(QuotaDate) AS SalesYear,Month(QuotaDate)\/3+1 AS SalesQuarter,\nSalesQuota AS CurrentQuota, LEAD(SalesQuota+5, 1,0) OVER (ORDER BY YEAR(QuotaDate)) AS NextQuota\nFROM Sales.SalesPersonQuotaHistory\nWHERE BusinessEntityID = 279 AND YEAR(QuotaDate) = 2006\nORDER BY SalesQuarter DESC<\/pre>\n<p id=\"XXNksDZ\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-8798  aligncenter\" src=\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/01\/img_5c51e5945f48a.png\" alt=\"\" width=\"799\" height=\"258\" \/><\/p>\n<p><strong>Partition By Clause:<\/strong><\/p>\n<p>When we examine the query and its result, we see that it is grouped according to the BusinessEntityID in Partition By Clause.<\/p>\n<pre class=\"lang:default decode:true \">SELECT BusinessEntityID,YEAR(QuotaDate) AS SalesYear,\nMonth(QuotaDate)\/3+1 AS SalesQuarter,SalesQuota AS CurrentQuota,\nLEAD(SalesQuota+5, 1,0) OVER (PARTITION BY BusinessEntityID ORDER BY YEAR(QuotaDate)) AS NextQuota\nFROM Sales.SalesPersonQuotaHistory\nWHERE YEAR(QuotaDate) = 2006 AND BusinessEntityID in (274,275)<\/pre>\n<p id=\"LJdQZtF\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-8799  aligncenter\" src=\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/01\/img_5c51ebd220e2f.png\" alt=\"\" width=\"799\" height=\"324\" \/><\/p>\n<h3>LAG Function in SQL Server(TSQL):<\/h3>\n<p>In contrast to the LEAD function, the LAG function allows a movement in the reverse direction.<\/p>\n<p>LAG function allows you to move backward from the current row within the targeted result set.\u00a0This action may be on the entire result set, or it may be on a specified part of the result set. In this way, we can compare the values in the table as we need without self-joining the table.<\/p>\n<p>Now let&#8217;s look at the basic template:<\/p>\n<pre class=\"lang:default decode:true\">LAG ( scalar_expression [ ,offset ] , [ default ] )\n\u00a0\u00a0\u00a0\u00a0OVER ([\u00a0partition_by_clause]\u00a0order_by_clause)<\/pre>\n<p>The rules in the LEAD function are valid here. In the examples in the lead function, it is enough to write PreviousQuota instead of NextQuota\u00a0 and LAG instead of LEAD.<\/p>\n<pre class=\"lang:default decode:true \">SELECT BusinessEntityID,YEAR(QuotaDate) AS SalesYear,\nMonth(QuotaDate)\/3+1 AS SalesQuarter,SalesQuota AS CurrentQuota,\nLAG(SalesQuota+5, 1,0) OVER (PARTITION BY BusinessEntityID ORDER BY YEAR(QuotaDate)) AS PreviousQuota\nFROM Sales.SalesPersonQuotaHistory\nWHERE YEAR(QuotaDate) = 2006 AND BusinessEntityID in (274,275)<\/pre>\n<p id=\"MLlVyhh\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-8800  aligncenter\" src=\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/01\/img_5c51eeeb04bf6.png\" alt=\"\" width=\"782\" height=\"296\" \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_8795\" class=\"pvc_stats all  \" data-element-id=\"8795\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/dbtut.com\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>As a consultant and training provider in business intelligence projects on SQL Server, some analytic functions I expected to be announced for a long time were announced with the SQL Server 2012 version. We will examine two of these 8 new analytic functions, which are announced with SQL Server 2012: LEAD and LAG. LEAD Function &hellip;<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_8795\" class=\"pvc_stats all  \" data-element-id=\"8795\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/dbtut.com\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":471,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[3,1596],"tags":[1861,1865,1863,1860,1864,1862],"class_list":["post-8795","post","type-post","status-publish","format-standard","","category-mssql","category-tsql","tag-lag-function","tag-lag-function-in-sql-server","tag-lag-function-in-tsql","tag-lead-function","tag-lead-function-in-sql-server","tag-lead-function-in-tsql"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>LEAD and LAG Functions in SQL Server(TSQL) - Database Tutorials<\/title>\n<meta name=\"description\" content=\"LEAD and LAG Functions in SQL Server(TSQL)\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"LEAD and LAG Functions in SQL Server(TSQL) - Database Tutorials\" \/>\n<meta property=\"og:description\" content=\"LEAD and LAG Functions in SQL Server(TSQL)\" \/>\n<meta property=\"og:url\" content=\"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/\" \/>\n<meta property=\"og:site_name\" content=\"Database Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-30T18:41:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-01-30T18:42:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/01\/img_5c51d6fb7ad35.png\" \/>\n<meta name=\"author\" content=\"Resul \u00c7AVU\u015eO\u011eLU\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Resul \u00c7AVU\u015eO\u011eLU\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/\"},\"author\":{\"name\":\"Resul \u00c7AVU\u015eO\u011eLU\",\"@id\":\"https:\/\/dbtut.com\/#\/schema\/person\/9aa581e3b03eac5044bb6e88eb239426\"},\"headline\":\"LEAD and LAG Functions in SQL Server(TSQL)\",\"datePublished\":\"2019-01-30T18:41:07+00:00\",\"dateModified\":\"2019-01-30T18:42:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/\"},\"wordCount\":555,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/dbtut.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/01\/img_5c51d6fb7ad35.png\",\"keywords\":[\"LAG Function\",\"LAG Function in SQL Server\",\"LAG Function in TSQL\",\"LEAD Function\",\"LEAD Function in SQL Server\",\"LEAD Function in TSQL\"],\"articleSection\":[\"MSSQL\",\"TSQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/\",\"url\":\"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/\",\"name\":\"LEAD and LAG Functions in SQL Server(TSQL) - Database Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/dbtut.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/01\/img_5c51d6fb7ad35.png\",\"datePublished\":\"2019-01-30T18:41:07+00:00\",\"dateModified\":\"2019-01-30T18:42:05+00:00\",\"description\":\"LEAD and LAG Functions in SQL Server(TSQL)\",\"breadcrumb\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/#primaryimage\",\"url\":\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/01\/img_5c51d6fb7ad35.png\",\"contentUrl\":\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/01\/img_5c51d6fb7ad35.png\",\"width\":910,\"height\":313},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/dbtut.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"LEAD and LAG Functions in SQL Server(TSQL)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/dbtut.com\/#website\",\"url\":\"https:\/\/dbtut.com\/\",\"name\":\"Database Tutorials\",\"description\":\"MSSQL, Oracle, PostgreSQL, MySQL, MariaDB, DB2, Sybase, Teradata, Big Data, NOSQL, MongoDB, Couchbase, Cassandra, Windows, Linux\",\"publisher\":{\"@id\":\"https:\/\/dbtut.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/dbtut.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/dbtut.com\/#organization\",\"name\":\"dbtut\",\"url\":\"https:\/\/dbtut.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/dbtut.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/dbtut.com\/wp-content\/uploads\/2021\/02\/dbtutlogo.jpg\",\"contentUrl\":\"https:\/\/dbtut.com\/wp-content\/uploads\/2021\/02\/dbtutlogo.jpg\",\"width\":223,\"height\":36,\"caption\":\"dbtut\"},\"image\":{\"@id\":\"https:\/\/dbtut.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/dbtut.com\/#\/schema\/person\/9aa581e3b03eac5044bb6e88eb239426\",\"name\":\"Resul \u00c7AVU\u015eO\u011eLU\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/dbtut.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4dfa29e15e6b78ed6ba3235d3bc8b3c1c6ff77abbfeab200700a5671181cd2cd?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4dfa29e15e6b78ed6ba3235d3bc8b3c1c6ff77abbfeab200700a5671181cd2cd?s=96&d=mm&r=g\",\"caption\":\"Resul \u00c7AVU\u015eO\u011eLU\"},\"url\":\"https:\/\/dbtut.com\/index.php\/author\/resulcavusoglu\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"LEAD and LAG Functions in SQL Server(TSQL) - Database Tutorials","description":"LEAD and LAG Functions in SQL Server(TSQL)","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/","og_locale":"en_US","og_type":"article","og_title":"LEAD and LAG Functions in SQL Server(TSQL) - Database Tutorials","og_description":"LEAD and LAG Functions in SQL Server(TSQL)","og_url":"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/","og_site_name":"Database Tutorials","article_published_time":"2019-01-30T18:41:07+00:00","article_modified_time":"2019-01-30T18:42:05+00:00","og_image":[{"url":"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/01\/img_5c51d6fb7ad35.png","type":"","width":"","height":""}],"author":"Resul \u00c7AVU\u015eO\u011eLU","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Resul \u00c7AVU\u015eO\u011eLU","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/#article","isPartOf":{"@id":"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/"},"author":{"name":"Resul \u00c7AVU\u015eO\u011eLU","@id":"https:\/\/dbtut.com\/#\/schema\/person\/9aa581e3b03eac5044bb6e88eb239426"},"headline":"LEAD and LAG Functions in SQL Server(TSQL)","datePublished":"2019-01-30T18:41:07+00:00","dateModified":"2019-01-30T18:42:05+00:00","mainEntityOfPage":{"@id":"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/"},"wordCount":555,"commentCount":0,"publisher":{"@id":"https:\/\/dbtut.com\/#organization"},"image":{"@id":"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/#primaryimage"},"thumbnailUrl":"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/01\/img_5c51d6fb7ad35.png","keywords":["LAG Function","LAG Function in SQL Server","LAG Function in TSQL","LEAD Function","LEAD Function in SQL Server","LEAD Function in TSQL"],"articleSection":["MSSQL","TSQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/","url":"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/","name":"LEAD and LAG Functions in SQL Server(TSQL) - Database Tutorials","isPartOf":{"@id":"https:\/\/dbtut.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/#primaryimage"},"image":{"@id":"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/#primaryimage"},"thumbnailUrl":"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/01\/img_5c51d6fb7ad35.png","datePublished":"2019-01-30T18:41:07+00:00","dateModified":"2019-01-30T18:42:05+00:00","description":"LEAD and LAG Functions in SQL Server(TSQL)","breadcrumb":{"@id":"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/#primaryimage","url":"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/01\/img_5c51d6fb7ad35.png","contentUrl":"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/01\/img_5c51d6fb7ad35.png","width":910,"height":313},{"@type":"BreadcrumbList","@id":"https:\/\/dbtut.com\/index.php\/2019\/01\/30\/lead-and-lag-functions-in-sql-servertsql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/dbtut.com\/"},{"@type":"ListItem","position":2,"name":"LEAD and LAG Functions in SQL Server(TSQL)"}]},{"@type":"WebSite","@id":"https:\/\/dbtut.com\/#website","url":"https:\/\/dbtut.com\/","name":"Database Tutorials","description":"MSSQL, Oracle, PostgreSQL, MySQL, MariaDB, DB2, Sybase, Teradata, Big Data, NOSQL, MongoDB, Couchbase, Cassandra, Windows, Linux","publisher":{"@id":"https:\/\/dbtut.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/dbtut.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/dbtut.com\/#organization","name":"dbtut","url":"https:\/\/dbtut.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/dbtut.com\/#\/schema\/logo\/image\/","url":"https:\/\/dbtut.com\/wp-content\/uploads\/2021\/02\/dbtutlogo.jpg","contentUrl":"https:\/\/dbtut.com\/wp-content\/uploads\/2021\/02\/dbtutlogo.jpg","width":223,"height":36,"caption":"dbtut"},"image":{"@id":"https:\/\/dbtut.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/dbtut.com\/#\/schema\/person\/9aa581e3b03eac5044bb6e88eb239426","name":"Resul \u00c7AVU\u015eO\u011eLU","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/dbtut.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4dfa29e15e6b78ed6ba3235d3bc8b3c1c6ff77abbfeab200700a5671181cd2cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4dfa29e15e6b78ed6ba3235d3bc8b3c1c6ff77abbfeab200700a5671181cd2cd?s=96&d=mm&r=g","caption":"Resul \u00c7AVU\u015eO\u011eLU"},"url":"https:\/\/dbtut.com\/index.php\/author\/resulcavusoglu\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/posts\/8795","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/users\/471"}],"replies":[{"embeddable":true,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/comments?post=8795"}],"version-history":[{"count":0,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/posts\/8795\/revisions"}],"wp:attachment":[{"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/media?parent=8795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/categories?post=8795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/tags?post=8795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}