Lead, LAG & CTE Victor Ivantsov
About VICTOR IVANTSOV Started with SQL Server 6.5 in 1996, still enjoy it Now on SQL 2016 Standard, SSIS, SSRS, T-SQL My Toastmaster club (Rivercity TM) meets every Tuesday @ 7:15 AM Enjoy running, soccer, music 904-351-6336 (voice, text) victor.ivantsov@gmail.com
Agenda Introduction: why I am addicted to cross/outer apply Deep dive #1: split string values into rows by delimiter Deep dive #2: support any user defined filter condition Adjournment, QnA
Big RecordsETS @dir @sort @total @limit @start
@sort As Column Expression
ORDER BY CLAUSE
SPLIT LEFT and RIGHT Values ‘Jacksonville, FL 33296’ ‘Smith Jr, Michael Peter’ ‘coverageAmount ( 7,500’ ‘UploadFile_usrid_12345.xls’ ‘3)’
Ugly & Buggy code
Reuse existing code Take existing function that split string values via XML Add item # to the list Escape special xml characters (&, <, > “) Use new LEAD sql keword
Split values via xml - Preparation
SPLIT VALUES – XML Ready
Split Values – Function Ready
LEAD – usage example https://docs.microsoft.com/en-us/sql/t-sql/functions/lead-transact-sql?view=sql-server-2017
Another complex function
Supported EXPRESSions Is_match = New computed column
LEFT OUTER JOIN SAME COMPLEX FUNCTION? SOLUTION #1 LEFT OUTER JOIN SAME COMPLEX FUNCTION? TOO MUCH COMPLEXITY FOR QUERY OPTIMIZER
MULTI STATEMENT FUNCTION SOLUTION #2 MULTI STATEMENT FUNCTION STILL TOO SLOW
LaG & CTE *
Q & A Victor Ivantsov 904-351-6336 (voice, text) victor.ivantsov@gmail.com Rivercity Toastmasters, club #892 meets every Tuesday @ 7:15 AM IHOP 3250 Hodges Blvd, Jacksonville, FL 32224