May 25, 2021 at 9:57 am
Hi everyone,
I am working with a recursive CTE that loops through records. I have lead and lag variables the issue is that my lag variables need to look back the number of steps the loop is in. I have the lag working fine but the lead works inversely to the lag. So when the lag is looking back 4 steps the lead is looking forward 1. When the lag is looking back 3 steps then the lead should be 2 and so on. Is there any online material where someone has dealt with this issue?
Thanks
May 25, 2021 at 11:02 am
I doubt you'll find a solution that is exactly specific to your needs. If you post some sample data (as DDL and DML statements in a code block), and expected results, along with what you have so far I am sure someone here'll be able to push you in the right direction or provide a solution.
Though, it sounds like, if I am honest, that the LEAD
is just the 5 minus the LAG
value.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
May 25, 2021 at 6:24 pm
I guess I don't understand the need to "loop through the records". A well written WHILE loop is usually faster and about 8 times less resource intensive. That, notwithstanding, why aren't you just using some nice, straight-forward set based code? What is it that your code is actually doing?
--Jeff Moden
Change is inevitable... Change for the better is not.
May 26, 2021 at 3:21 am
Sorry I could not include the data it was a work project. I've simplified the CTE so now I don't need the lead so everything is working. I am going to put it into a multi table UDF function so it is reusable.
Thanks
May 26, 2021 at 7:59 am
Sorry I could not include the data it was a work project. I've simplified the CTE so now I don't need the lead so everything is working. I am going to put it into a multi table UDF function so it is reusable.
Thanks
If you're making a function, don't make a multi-line table value function, make it in an inline table value function; the former can perform very poorly.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
May 26, 2021 at 8:03 am
Is there any resources for learning UDFs? I have look online but I could only find basic introductions. I am keen to fully understand how I can best use them
May 26, 2021 at 8:03 am
Alas, SQL Server's performance can suffer from encapsulating in UDF
May 27, 2021 at 10:43 am
Is there any resources for learning UDFs? I have look online but I could only find basic introductions. I am keen to fully understand how I can best use them
Here's an article on one of the more important aspects of "UDFs" in SQL Server. And, no... I wouldn't trust SQL Server 2019 to "do the needy" thing for scalar of mTVFs (Multi-Statement Table Valued Functions).
https://www.sqlservercentral.com/articles/how-to-make-scalar-udfs-run-faster-sql-spackle
--Jeff Moden
Change is inevitable... Change for the better is not.
May 27, 2021 at 12:06 pm
Sorry I could not include the data it was a work project. I've simplified the CTE so now I don't need the lead so everything is working. I am going to put it into a multi table UDF function so it is reusable.
Thanks
If you're using a Recursive CTE (rCTE) to "loop" through rows one at a time, you're probably dooming your project to being slow and very resource intensive. The same holds true if you use a Multi-statement Table Valued Function (mTVF).
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply