Rechercher dans le manuel MySQL
12.21.4 Named Windows
Windows can be defined and given names by which to refer to them
in OVER
clauses. To do this, use a
WINDOW
clause. If present in a query, the
WINDOW
clause falls between the positions of
the HAVING
and ORDER BY
clauses, and has this syntax:
For each window definition,
window_name
is the window name, and
window_spec
is the same type of
window specification as given between the parentheses of an
OVER
clause, as described in
Section 12.21.2, “Window Function Concepts and Syntax”:
- window_spec:
- [window_name] [partition_clause] [order_clause] [frame_clause]
A WINDOW
clause is useful for queries in
which multiple OVER
clauses would otherwise
define the same window. Instead, you can define the window once,
give it a name, and refer to the name in the
OVER
clauses. Consider this query, which
defines the same window multiple times:
- val,
- FROM numbers;
The query can be written more simply by using
WINDOW
to define the window once and
referring to the window by name in the OVER
clauses:
- val,
- FROM numbers
A named window also makes it easier to experiment with the
window definition to see the effect on query results. You need
only modify the window definition in the
WINDOW
clause, rather than multiple
OVER
clause definitions.
If an OVER
clause uses OVER
(
rather
than window_name
...)OVER
, the named
window can be modified by the addition of other clauses. For
example, this query defines a window that includes partitioning,
and uses window_name
ORDER BY
in the
OVER
clauses to modify the window in
different ways:
- FROM sales
An OVER
clause can only add properties to a
named window, not modify them. If the named window definition
includes a partitioning, ordering, or framing property, the
OVER
clause that refers to the window name
cannot also include the same kind of property or an error
occurs:
This construct is permitted because the window definition and the referring
OVER
clause do not contain the same kind of properties:This construct is not permitted because the
OVER
clause specifiesPARTITION BY
for a named window that already hasPARTITION BY
:
The definition of a named window can itself begin with a
window_name
. In such cases, forward
and backward references are permitted, but not cycles:
Traduction non disponible
Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-window-functions-named-windows.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.