Joining Time-Series Data

You can use the ASOF JOIN construct to join tables that contain time-series data. ASOF JOIN is available as standard syntax within the FROM clause of a SELECT statement. Although ASOF JOIN queries can be emulated through the use of complex SQL, other types of joins, and window functions, these queries are easier to write (and are optimized) if you use the ASOF JOIN syntax.

Introduction

You can use ASOF joins to analyze time-series data, such as financial trading data. Transaction-cost analysis, for example, requires « slippage » calculations, which measure the difference between the price quoted at the time of a decision to buy stocks and the price actually paid when the trade was executed and recorded. The ASOF JOIN can expedite this type of analysis. Given that the key capability of this join method is the analysis of one time series with respect to another, ASOF JOIN may be useful for analyzing any data set that is historical in nature, such as weather observations, readings from sensors, or audit trails. In many of these use cases, ASOF JOIN may be used to associate data when readings from different devices have timestamps that are not exactly the same.

On suppose que les données de série chronologique que vous devez analyser existent dans deux tables, et qu’il existe un horodatage pour chaque ligne de chaque table. Cet horodatage représente la date et l’heure « à compter de » précises d’un événement enregistré. Pour chaque ligne de la première table (ou table de gauche), la jointure utilise une « condition de correspondance » avec un opérateur de comparaison que vous spécifiez pour trouver une seule ligne dans la deuxième table (ou table de droite) où la valeur de l’horodatage est l’une des suivantes :

  • Inférieure ou égale à la valeur d’horodatage dans la table de gauche.

  • Supérieure ou égale à la valeur d’horodatage dans la table de gauche.

  • Inférieure à la valeur d’horodatage dans la table de gauche.

  • Supérieure à la valeur d’horodatage dans la table de gauche.

La ligne éligible située à droite est la correspondance la plus proche, qui peut être égale dans le temps, antérieure dans le temps ou postérieure dans le temps, selon l’opérateur de comparaison spécifié.

The cardinality of the result of the ASOF JOIN is always equal to the cardinality of the left table. If the left table contains 40 million rows, the ASOF JOIN returns 40 million rows. Therefore, the left table may be thought of as the « preserving » table, and the right table as the « referenced » table.

Conceptual Example of an ASOF JOIN Query

For example, in a financial application, you might have a table named quotes and a table named trades. One table records the history of bids to buy stock, and the other records the history of actual trades. A bid to buy stocks happens before the trade (or possibly at the « same » time, depending on the granularity of the recorded time). Both tables have timestamps, and both have other columns of interest that you may want to compare. A simple ASOF JOIN query will return the closest quote (in time) prior to each trade. In other words, the query asks: What was the price of a given stock at the time I made a trade?

Supposons que la table trades contienne trois lignes et que la table quotes en contienne sept. La couleur d’arrière-plan des cellules indique les trois lignes de quotes qui seront éligibles à ASOF JOIN lorsque les lignes seront jointes sur des symboles boursiers correspondants et que leurs colonnes d’horodatage seront comparées.

Table TRADES (table gauche ou « préservante »)

Données de la table de transactions, composées de trois lignes, qui sont jointes à trois lignes de la table de valeurs cotées.

Table QUOTES (table droite ou « référencée »)

Données de la table de valeurs cotées, composées de sept lignes, identifiant les trois lignes spécifiques qui sont éligibles à la jointure avec la table de valeurs cotées.

To see the syntax that produces the combination of these three highlighted rows (and many other examples), see Join with Match and ON Conditions.