patternsqlMajor
MySQL and window functions
Viewed 0 times
andwindowmysqlfunctions
Problem
It seems that
E.g. the simple:
What I am not sure is if this applies to commercial version as well (I assume the community version is limited subset).
If not, how does one work around this missing feature?
MySQL does not support window functions.E.g. the simple:
COUNT(*) OVER() AS cnt does not work.What I am not sure is if this applies to commercial version as well (I assume the community version is limited subset).
If not, how does one work around this missing feature?
Solution
MySQL does not support Window Functions(*). There is what we call "a poor man's window function" in the form of GROUP_CONCAT().
There are plenty of tricks using
In particular, Selecting a specific non aggregated column data in GROUP BY and SQL: selecting top N records per group, another solution might be of interest to you and could give you a kick start.
Things you should note about
(*) Support for Window Functions has been added in MySQL 8
There are plenty of tricks using
GROUP_CONCAT to emulate window functions. They are not as pretty (syntactically) and are sometimes too limited. I've written a few. See my blog post complaining about the missing window functions, and linking to various solutions based on GROUP_CONCAT.In particular, Selecting a specific non aggregated column data in GROUP BY and SQL: selecting top N records per group, another solution might be of interest to you and could give you a kick start.
Things you should note about
GROUP_CONCAT():- Can use
DISTINCT
- Can use
ORDER BY ... ASC/DESC
- Can set
SEPARATOR
- As any aggregation function - it discards NULL values; plenty tricks on that.
(*) Support for Window Functions has been added in MySQL 8
Context
StackExchange Database Administrators Q#40130, answer score: 36
Revisions (0)
No revisions yet.