Nesse vídeo apresentamos como contar os dias entre datas de registros diferentes, nesse exemplo, calcular a quantidade de dias entre as alterações ocorridas em versões de documentos
1. Get value from previous record (LAG, OVER, PARTITION, ORDER BY):
The LAG function is used to get the value of a column from the previous row in a result set. The OVER clause with the PARTITION BY and ORDER BY options helps define the window in which the LAG function operates. This is useful for calculating differences or trends over consecutive records.
Example:
SELECT Column1, LAG(Column1) OVER (PARTITION BY Category ORDER BY DateColumn) AS PreviousValue FROM YourTable;
2. Count the number
The DATEDIFF function is used to calculate the difference between two dates in terms of a specific unit, such as days, months, or years.
SELECT DATEDIFF(DAY, StartDate, EndDate) AS DayDifference FROM YourTable;
3. Handle Registry Null Values (
The ISNULL function is used to replace null values with an alternate value. This is useful for ensuring that the query results do not contain unwanted null values.
SELECT Column1, ISNULL(Column1, Alternate Value) AS NonNullValue FROM YourTable;
In this example, if Column1 is null, 'Alternate Value' is returned.
These functionalities are commonly used for data manipulation and analysis in Microsoft SQL Server.
Data Scientist and Consultant for Digital and Analytics Solutions
@fabioms