CData Connectors Support the LAG() Window Function

CData Drivers & Connectors include support for the LAG() window function for more comprehensive and powerful querying capabilities.

Date Entered: 01/31/2025    Last Updated: 01/31/2025

CData Drivers, Connectors, and other solutions now include the LAG() window function in the library of supported SQL functions. This capability unlocks the ability to compare and calculate the change in values over a sequence of data. This support is across all data sources. Where possible, CData solutions will push the LAG request down to the data source, otherwise the function will be resolved in memory after the initial data is retrived.

Example Query Using the LAG() Function

The example below uses the Data Explorer in CData Connect Cloud with a Salesforce connection, but the principals apply to any CData solution across all data sources.

The query below calculates the sum of all expected revenue for all opportunities, grouped by month of the opportunity and the associated customer account in Salesforce. Using the LAG() function, we compare each month's expected revenue to the previous month.

Query

SELECT Account.Name, MONTH(CloseDate) AS CloseMonth, LAG(SUM(Revenue), 1, 0) OVER ( PARTITION BY AccountId ORDER BY CloseDate) AS PreviousRevenue, SUM(Revenue) AS CurrentRevenue FROM Salesforce1.Salesforce.Opportunity JOIN Salesforce1.Salesforce.Account on Opportunity.AccountId = Account.Id WHERE MONTH(CloseDate) IN ( '3', '4', '5', '6', '7', '8' ) AND AccountId = '0016A000003FZacQAG' GROUP BY AccountId, MONTH(CloseDate)

Results

The results below show the first 10 rows of the response from Salesforce. Not each Month is consecutive, representing accounts where there may not have been opportunities for the months in between.

NameCloseMonthPreviousRevenueCurrentRevenue
Burlington Textiles Corp of America3029999
Burlington Textiles Corp of America429999872303.85
Burlington Textiles Corp of America5872303.8536624.2
Burlington Textiles Corp of America636624.272666.1
Burlington Textiles Corp of America772666.18843.4
Burlington Textiles Corp of America88843.495835.3

We appreciate your feedback.  If you have any questions, comments, or suggestions about this entry, please contact our support team at [email protected].