userdefined function cross apply all days into the future sql

USE Northwind	
GO

SELECT o1.OrderID, o1.OrderDate, ca.OrderID AS NextOrder, 
		ca.OrderDate AS NextOrderDate, CustomerID, 
			DATEDIFF(DAY, o1.OrderDate,ca.OrderDate) DaysToNextOrder
FROM Orders AS o1
	CROSS APPLY
			(SELECT TOP 1 o.OrderDate, o.OrderID
			 FROM Orders AS o 
                         WHERE o.customerID = o1.customerID
				AND o.OrderID > o1.OrderID
			 ORDER BY OrderID) AS ca

ORDER BY CustomerID, o1.OrderID

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source