显示3个或更多连续行(Sql)

时间:2017-12-26 14:04:09

标签: sql oracle oracle11g

我有一张包含以下数据的表格

+------+------------+-----------+
| id   | date1      | people    |
+------+------------+-----------+
| 1    | 2017-01-01 | 10        |
| 2    | 2017-01-02 | 109       |
| 3    | 2017-01-03 | 150       |
| 4    | 2017-01-04 | 99        |
| 5    | 2017-01-05 | 145       |
| 6    | 2017-01-06 | 1455      |
| 7    | 2017-01-07 | 199       |
| 8    | 2017-01-08 | 188       |
+------+------------+-----------+

现在我要做的是显示连续3行,其中人们> = 100就像这样

+------+------------+-----------+
| id   | date1      | people    |
+------+------------+-----------+
| 5    | 2017-01-05 | 145       |
| 6    | 2017-01-06 | 1455      |
| 7    | 2017-01-07 | 199       |
| 8    | 2017-01-08 | 188       |
+------+------------+-----------+

任何人都可以帮助我如何使用oracle数据库进行此查询。我能够显示超过100但不是连续的行

创建表格(减少将要帮助的人的打字时间)

CREATE TABLE stadium
   ( id int  
   , date1 date, people int 
   );

    Insert into stadium values (    
 1,TO_DATE('2017-01-01','YYYY-MM-DD'),10);

Insert into stadium values       
 (2,TO_DATE('2017-01-02','YYYY-MM-DD'),109);

Insert into stadium values(       
 3,TO_DATE('2017-01-03','YYYY-MM-DD'),150);

Insert into stadium values(       
 4,TO_DATE('2017-01-04','YYYY-MM-DD'),99);

Insert into stadium values(     
 5,TO_DATE('2017-01-05','YYYY-MM-DD'),145);

Insert into stadium values(  
 6,TO_DATE('2017-01-06','YYYY-MM-DD'),1455);

Insert into stadium values
(7,TO_DATE('2017-01-07','YYYY-MM-DD'),199);

Insert into stadium values(
 8,TO_DATE('2017-01-08','YYYY-MM-DD'),188);

提前感谢您的帮助

6 个答案:

答案 0 :(得分:0)

假设你的意思是> = 100,有几种方法。一种方法只使用lead()lag()。但是,一种简单的方法通过值的数量来定义每个组> = 100。 100之前。然后它使用count(*)来查找连续值的大小:

select s.*
from (select s.*, count(*) over (partition by grp) as num100pl
      from (select s.*,
                   sum(case when people < 100 then 1 else 0 end) over (order by date) as grp
            from stadium s
           ) s
     ) s
where num100pl >= 3;

Here是一个SQL小提琴,显示语法有效。

答案 1 :(得分:0)

我假设$scope.fieldOne = $scope.businessDetails.post_meta_fields["_ait-item_item-data‌​"] id列都是连续的并且相互对应(如果date,则需要额外的ROW_NUMBER() s与日期不是顺序的,如果日期不一定是连续的,则包括更复杂的逻辑。

id

产生以下输出:

SELECT 
    *

FROM 
    (
        SELECT
            *
            ,COUNT(date) OVER (PARTITION BY sequential_group_num) AS num_days_in_sequence

        FROM 
            (
                SELECT
                    *
                    ,(id - ROW_NUMBER() OVER (ORDER BY date)) AS sequential_group_num

                FROM
                    stadium

                WHERE 
                    people >= 100

            ) AS subquery1

    ) AS subquery2

WHERE 
    num_days_in_sequence >= 3

答案 2 :(得分:0)

通过使用连接,我们可以显示像这样的连续行

count

答案 3 :(得分:0)

select distinct
   t1.* 
from
   stadium t1 
   join
      stadium t2 
   join
      stadium t3 
where
   t1.people >= 100 
   and t2.people >= 100 
   and t3.people >= 100 
   and 
   ( 
(t1.id + 1 = t2.id 
      and t2.id + 1 = t3.id) 
      or 
      (
         t2.id + 1 = t1.id 
         and t1.id + 1 = t3.id
      )
      or 
      (
         t2.id + 1 = t3.id 
         and t3.id + 1 = t1.id
      )
   )
order by
   id;

答案 4 :(得分:0)

SQL脚本:

SELECT DISTINCT SS.*
FROM STADIUM SS
INNER JOIN 
(SELECT S1.ID
    FROM STADIUM S1
    WHERE 3 = (
    SELECT COUNT(1)
    FROM STADIUM S2
    WHERE (S2.ID=S1.ID OR S2.ID=S1.ID+1 OR S2.ID=S1.ID+2)
    AND S2.PEOPLE >= 100
    )) AS SS2
    ON SS.ID>=SS2.ID AND SS.ID<SS2.ID+3

答案 5 :(得分:0)

选择* 从( select * , count(*) over (partition by grp) as total 来自
(select * , Sum(case when people < 100 then 1 else 0 end) over (order by date) as grp 来自体育场)T - 内部查询 1 where people >=100 )S--内部查询2 where total >=3 --outer query

enter image description here