I have a query that is tracking production. Whenever a unit is produced I capture the timestamp, part number, delta time, and what the cycle time should have been, as well as others that aren’t important to list here. In a SSRS report I count the rows and group them by hour and run efficiency calculations each hour based on the cycle times divided by the deltas. What I need is to add a column in my select statement that will flag whenever a part number changes so I can identify when a changeover has occurred by referencing it in my report.
Example: Select part number of row – part number of previous row as changeover
Every row would have a value that would always be 0 unless a changeover occurred then it would be a different value for that one row. I could have my report do its thing on any value <> 0.
As noted by SQLHound, if you're using SQL Server 2012 or another database that supports the LAG function then you can use this in your SQL query.
A SQL Server example:
see it on SQL Fiddle
If you're source database doesn't support LAG, then can you tell us what database/version it is so we can try to provide an alternative solution.
Update: Additional example using a CTE, for SQL Server versions prior to 2012:
See it on SQL Fiddle