Hi Martijn, Thanks for your quick response. I tried it, but it does not seem to work.
The problem is that I want to select fields that are not in the `GROUP BY`. So in my example, I can have a tumble window on `readtime`, and select max(gauge), but I also want both `deviceId` and `locationId` of the max record included in the selected result. With Top-N, it does not seem to allow that. -Guoqin On Wed, Dec 8, 2021 at 1:22 PM Martijn Visser <mart...@ververica.com> wrote: > Hi Guoqin, > > I think you could use the Window Top-N. There's a recipe in the Flink SQL > Cookbook [1]. The example uses a SUM which you should change to MAX and of > course you change the rownum to 1 instead of 3. > > Best regards, > > Martijn > > [1] > > https://github.com/ververica/flink-sql-cookbook/blob/main/aggregations-and-analytics/11_window_top_n/11_window_top_n.md > > Op wo 8 dec. 2021 om 19:54 schreef Guoqin Zheng <lanson.zh...@gmail.com> > >> Hi Flink Community, >> >> I am curious what the recommended way is to select the event with a max >> attribute value with SQL api. >> >> For example, I have an event stream like: >> >> { >> deviceId, >> locationId >> gauge, >> readtime, <-- eventTime >> } >> >> I want to figure out which device and location has the max gauge over a >> 5-mins window. >> >> Any advice would be greatly appreciated! >> >> Thanks! >> -Guoqin >> > -- > > Martijn Visser | Product Manager > > mart...@ververica.com > > <https://www.ververica.com/> > > > Follow us @VervericaData > > -- > > Join Flink Forward <https://flink-forward.org/> - The Apache Flink > Conference > > Stream Processing | Event Driven | Real Time > >