Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Set DateTime = To DateTime Of prevuious row on a view

Below is the code to a view created to pull delimitated values from a table into new rows in a view.

This works well, but i cant figure out how to set the TASDateTimeStart Column = to the previous rows TASDateTimeEnd Column where the id in both rows are the same

SELECT
  MallaghanApp.dbo.EmployeeClockIn.Id,
  Employee,
  JobType,
  EmployeeName,
  EmployeeNumber,
  value AS SerialNumber,
  cast([TASDateTimeEnd] AS smalldatetime) [TASDateTimeEnd],
  cast([TASDateTimeStart] AS smalldatetime) [TASDateTimeStart],
  ISNULL(
         ((SELECT        DATEDIFF(MINUTE, TASDateTimeStart, TASDateTimeEnd)) /
         ((SELECT        len([SerialNo]) - len(replace([SerialNo], ',', ''))) + 1))
        , (DATEDIFF(MINUTE, TASDateTimeStart, CURRENT_TIMESTAMP)) /
         ((SELECT        len([SerialNo]) - len(replace([SerialNo], ',', ''))) + 1)) AS MinutesClocked,
  Department,
  DepartmentNumber
FROM EmployeeClockIn
CROSS APPLY STRING_SPLIT([SerialNo], ',') 
LEFT JOIN EmployeeInfo ON Employee = EmployeeCombinedInfo

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I keep getting the error "Subquery returned more than 1 value.This is not permitted when the subquery follows =,!=,<,<=,>,>= or when the subquery is used as an expression"

Anyone know the code needed here?

>Solution :

You can use the LAG function to get this value:

SELECT eci.Id, 
        eci.TASDateTimeEnd,
        LAG(eci.TASDateTimeEnd) OVER (PARTITION BY eci.Id ORDER BY eci.TASDateTimeEnd) as PreviousTASDateTimeEnd
            
FROM EmployeeClockIn eci;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading