luoyh opened a new issue, #46604:
URL: https://github.com/apache/doris/issues/46604

   ### Search before asking
   
   - [X] I had searched in the 
[issues](https://github.com/apache/doris/issues?q=is%3Aissue) and found no 
similar issues.
   
   
   ### Version
   
   doris-2.1.6-rc04-653e315ba5
   
   ### What's Wrong?
   
   test table:
   ```sql
   CREATE TABLE `tmp_sum_bug` (
     `device_id` bigint NOT NULL,
     `evt_time` datetime NOT NULL,
     `evt_state` int NOT NULL COMMENT ''
   ) ENGINE=OLAP
   UNIQUE KEY(`device_id`, `evt_time`)
   COMMENT 'test table'
   DISTRIBUTED BY HASH(`device_id`, `evt_time`) BUCKETS AUTO
   PROPERTIES (
   "replication_allocation" = "tag.location.default: 1"
   );
   
   insert into tmp_sum_bug(device_id,evt_time,evt_state) values
   (1,'2024-10-01 00:00:00',0),
   (1,'2024-10-01 00:10:00',0),
   (1,'2024-10-01 00:20:00',0),
   (1,'2024-10-01 00:30:00',0);
   
   mysql> select TIMESTAMPDIFF(second,'2024-10-01 00:00:00','2024-10-01 
23:59:59') dif;
   +-------+
   | dif   |
   +-------+
   | 86399 |
   +-------+
   1 row in set (0.03 sec)
   
   mysql> with group_duration as (
     select 
         device_id,
         TIMESTAMPDIFF(
         SECOND,
         evt_time,
         ifnull(
           lead(evt_time, 1, null) over(partition by device_id order by 
evt_time),
           '2024-10-01 23:59:59')
         ) as duration,
         evt_time,
         evt_state
     from tmp_sum_bug
   )
   , sum_duration as (
     select 
       device_id,
       sum(duration) over(
         partition by device_id,evt_state
         order by evt_time 
         rows between UNBOUNDED PRECEDING and current row
       ) duration,
       evt_state,
       evt_time,
       ROW_NUMBER() OVER (PARTITION BY device_id ORDER BY evt_time DESC) rn
     from group_duration 
   )
   select * from sum_duration ;
   +-----------+----------+-----------+---------------------+----+
   | device_id | duration | evt_state | evt_time            | rn |
   +-----------+----------+-----------+---------------------+----+
   |         1 |    86399 |         0 | 2024-10-01 00:30:00 |  1 |
   |         1 |     1800 |         0 | 2024-10-01 00:20:00 |  2 |
   |         1 |     1200 |         0 | 2024-10-01 00:10:00 |  3 |
   |         1 |      600 |         0 | 2024-10-01 00:00:00 |  4 |
   +-----------+----------+-----------+---------------------+----+
   4 rows in set (0.08 sec)
   
   mysql> with group_duration as (
     select 
         device_id,
         TIMESTAMPDIFF(
         SECOND,
         evt_time,
         ifnull(
           lead(evt_time, 1, null) over(partition by device_id order by 
evt_time),
           '2024-10-01 23:59:59')
         ) as duration,
         evt_time,
         evt_state
     from tmp_sum_bug
   )
   , sum_duration as (
     select 
       device_id,
       sum(duration) over(
         partition by device_id,evt_state
         order by evt_time 
         rows between UNBOUNDED PRECEDING and current row
       ) duration,
       evt_state,
       evt_time,
       ROW_NUMBER() OVER (PARTITION BY device_id ORDER BY evt_time DESC) rn
     from group_duration 
   )
   select * from sum_duration where rn=1;
   +-----------+----------+-----------+---------------------+----+
   | device_id | duration | evt_state | evt_time            | rn |
   +-----------+----------+-----------+---------------------+----+
   |         1 |    84599 |         0 | 2024-10-01 00:30:00 |  1 |
   +-----------+----------+-----------+---------------------+----+
   1 row in set (0.09 sec)
   
   ```
   add where rn=1, the result has error.
   
   
   ### What You Expected?
   
   the correct result should be:
   +-----------+----------+-----------+---------------------+----+
   | device_id | duration | evt_state | evt_time            | rn |
   +-----------+----------+-----------+---------------------+----+
   |         1 |    86399 |         0 | 2024-10-01 00:30:00 |  1 |
   
   ### How to Reproduce?
   
   _No response_
   
   ### Anything Else?
   
   _No response_
   
   ### Are you willing to submit PR?
   
   - [ ] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [X] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to