我目前有一个表,需要根据时间排序,然后取出当某字段为true时的前N条数据。
例如:
date name status
2014-01-01 0 FALSE
2014-01-02 0 FALSE
2014-01-03 0 FALSE
2014-01-04 0 FALSE
2014-01-05 0 TRUE
2014-01-06 0 FALSE
2014-01-01 1 FALSE
2014-01-02 1 FALSE
2014-01-03 1 FALSE
2014-01-04 1 TRUE
2014-01-05 1 FALSE
2014-01-06 1 FALSE
根据name进行分组,分组后根据date进行排序,取当status为true的前N条数据。
预期结果为:
date name status
2014-01-01 0 FALSE
2014-01-02 0 FALSE
2014-01-03 0 FALSE
2014-01-04 0 FALSE
2014-01-05 0 TRUE
2014-01-01 1 FALSE
2014-01-02 1 FALSE
2014-01-03 1 FALSE
2014-01-04 1 TRUE
求ORACLE 应该怎么写???
select t.date, t.name, t.status from TABLE1 t inner join (select Min(date) date, name from TABLE1 where status = 'TRUE' group by name) tt on t.date <= tt.date and t.name = tt.name order by t.name,t.date
看楼上的
1L正解!
1 select * from (select date,name,status,row_number() over(partition by name order by date ) as rId from tbl where status='true' ) where rId<10
1L