Is it possible to use MyBatis dynamic SQL to write SQL window functions?
project00unavailable opened this issue · 2 comments
Is it possibel to use MyBatis dynamic SQL to write SQL with SQL window functions like below?
SELECT start_terminal,
duration_seconds,
SUM(duration_seconds) OVER
(PARTITION BY start_terminal ORDER BY start_time)
AS running_total
FROM tutorial.dc_bikeshare_q1_2012
WHERE start_time < '2012-01-08'
There is no built in support for this, but you can write a function to accomplish this. See this page in the documentation: https://mybatis.org/mybatis-dynamic-sql/docs/extending.html
There are many functions already included in the library that you could use as starting points too...like the current sum function here: https://github.com/mybatis/mybatis-dynamic-sql/blob/master/src/main/java/org/mybatis/dynamic/sql/select/aggregate/Sum.java
There will be a change to the rendering process in the next version of the library for these types of functions that will allow more flexibility in rendering, but you can safely implement such a function today based on the current documentation. Functions written that way will continue to work for quite some time, and migration to the new method will be very straight forward. Watch #655 for the update.
The sum function has been updated to show the new rendering process. Let us know if you need help in writing your function.