SQL Training Online

  • Training Courses
  • Beginner Book
  • Blog

August 28, 2017 by Joey Blue 1 Comment

Prior Day Profit using the Lag Function with Windowing in SQL Server

You can get a prior day value using the Lag Function in SQL Server.

In this video I show you how to create a prior day squeeze of profit using the Windowing Lag function in TSQL.

This video builds on the prior day’s video, Moving Average with Windowing.

Video: Prior Day Profit using the Lag Function in SQL Server


How to use the SQL Lag Function for Prior Day Squeeze.

I use the Wide World Importers and the following scripts:

select 
[Invoice Date Key],
Profit,
Lag(Profit,1) OVER (ORDER BY [Invoice Date Key]) as PriorDayProfit,
Profit - isnull(Lag(Profit,1) OVER (ORDER BY [Invoice Date Key]),0) as DailySqueeze
from dbo.ProfitByDate
order by 1

Link to the Microsoft SQL Server Over Clause.

 

See the original SQL Prior Day Profit – Lag Windowing Function video.

Related

Filed Under: SQL Tip Tagged With: Over Clause, SQL INTO, SQL ISNULL Function, SQL Lag, SQL Server, Subtraction, Windowing Functions

Trackbacks

  1. Year-to-Date using Partition By with Windowing - SQL Training Online says:
    August 29, 2017 at 3:46 pm

    […] video builds on the prior day’s video, Prior Day Profit using the Lag Function with Windowing […]

    Reply

Leave a Question, Comment, or Reply. All are welcome!Cancel reply

Recent Posts

  • SQL Database Normalization – 1NF, 2NF, 3NF, and 4NF
  • SQL Joins Tutorial for Beginners – Inner Join, Left Join, Right Join, Full Outer Join – SQL Training Online
  • Zillow House Price Analysis from CSV Archive – #PowerBI 002
  • Learn Basic SQL – 1 Hour Training Course – SQL Training Online
  • Create Table Statement in SQL Server and Inserting Baseball Homerun Leader Dataset – SQL Training Online

Popular Posts

  • SQL Functions
  • SQL Jobs in Oracle and Microsoft SQL Server
  • Troubleshooting a SQL Join
Copyright © 2025 · SQLTrainingOnline.com · Consulting at EmbarkBlue.com