Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 代码>或月份。 Id, value, month,year 1, 123, oct, 2020 1, 128, nov, 2020 1, 127, jan ,2021 2, 121, Dec, 2020 2, 154, _Sql_Oracle_Yearmonth - Fatal编程技术网

Sql 代码>或月份。 Id, value, month,year 1, 123, oct, 2020 1, 128, nov, 2020 1, 127, jan ,2021 2, 121, Dec, 2020 2, 154,

Sql 代码>或月份。 Id, value, month,year 1, 123, oct, 2020 1, 128, nov, 2020 1, 127, jan ,2021 2, 121, Dec, 2020 2, 154, ,sql,oracle,yearmonth,Sql,Oracle,Yearmonth,代码>或月份。 Id, value, month,year 1, 123, oct, 2020 1, 128, nov, 2020 1, 127, jan ,2021 2, 121, Dec, 2020 2, 154, jan, 2021 SQL> with test (id, value, month, year) as 2 -- sample data; you have that, don't type it 3

代码>或
月份
Id, value, month,year
1,  123,    oct, 2020
1,  128,    nov, 2020
1,  127,    jan ,2021
2,  121,    Dec, 2020
2,   154,   jan,  2021   
SQL> with test (id, value, month, year) as
  2    -- sample data; you have that, don't type it
  3    (select 1, 123, 'oct', 2020 from dual union all
  4     select 1, 128, 'nov', 2020 from dual union all
  5     select 1, 127, 'jan', 2021 from dual union all
  6     select 2, 121, 'dec', 2020 from dual union all
  7     select 2, 154, 'jan', 2021 from dual
  8    ),
  9  temp as
 10    -- "convert" month and year to real date value
 11    (select id,
 12            value,
 13            to_date(month ||' '|| year, 'mon yyyy', 'nls_date_language=english') datum
 14     from test
 15    ),
 16  temp2 as
 17    -- select difference in months between DATUM and next month (LEAD!)
 18    (select id,
 19       months_between
 20         (datum,
 21          to_date(month ||' '|| year, 'mon yyyy', 'nls_date_language=english') datum
 22         ) diff
 23     from temp
 24    )
 25  select distinct id
 26  from temp2
 27  where abs(diff) > 1;

        ID
----------
         1

SQL>
select t.*
from (select t.*,
             lag(dte) over (partition by id order by dte) as prev_dte
      from (select t.*,
                   to_date(year || '-' || month || '-01', 'YYYY-MON-DD') as dte
            from t
           ) t
     ) t
where prev_dte <> dte - interval '1' month;