根据状态变化情况,求最大值和最小值

题目1:根据第一二列,计算出第三列。即:求每组KH_VALUE状态(1和0)变化的最小时间

--创建测试表
create table tmp as
select to_date(‘2017-04-21 16:22:00‘,‘yyyy-mm-dd hh24:mi:ss‘) dt, 1 kv from dual union all
select to_date(‘2017-04-21 16:23:00‘,‘yyyy-mm-dd hh24:mi:ss‘) dt, 1 kv from dual union all
select to_date(‘2017-04-21 16:24:00‘,‘yyyy-mm-dd hh24:mi:ss‘) dt, 0 kv from dual union all
select to_date(‘2017-04-21 16:25:00‘,‘yyyy-mm-dd hh24:mi:ss‘) dt, 0 kv from dual union all
select to_date(‘2017-04-21 16:26:00‘,‘yyyy-mm-dd hh24:mi:ss‘) dt, 0 kv from dual union all
select to_date(‘2017-04-21 16:27:00‘,‘yyyy-mm-dd hh24:mi:ss‘) dt, 0 kv from dual union all
select to_date(‘2017-04-21 16:28:00‘,‘yyyy-mm-dd hh24:mi:ss‘) dt, 1 kv from dual union all
select to_date(‘2017-04-21 16:29:00‘,‘yyyy-mm-dd hh24:mi:ss‘) dt, 1 kv from dual union all
select to_date(‘2017-04-21 16:30:00‘,‘yyyy-mm-dd hh24:mi:ss‘) dt, 0 kv from dual union all
select to_date(‘2017-04-21 16:31:00‘,‘yyyy-mm-dd hh24:mi:ss‘) dt, 0 kv from dual;
--SQL实现:
select dt,kv,min(dt)over(partition by rn order by dt) new_dt
 from(select dt,kv,sum(kv2)over(order by dt) rn
        from(select dt,kv,
                    --case when lag(kv,1)over(order by dt) = kv then 0 else 1 end kv2
                    case when lag(kv,1)over(order by dt) = kv then 0 else row_number()over(order by dt) end kv2
               from tmp
             )
      )

题目2:按照c1的顺序,求出c2状态发生变化的开始及结束位置。

已知tmp表数据如下:
c1 c2
------
1 1
2 1
4 1
5 0
6 0
7 0
8 1
9 1
10 1
11 1
12 1
13 0
14 1
15 1
16 1
17 1
18 1
19 1

c1列为编号,c2为状态(0,1),要求实现下面的效果:

开始位置,结束位置,状态
1,4,1
5,7,0
8,12,1
13,13,0,
14,19,1

--创建测试表
create table tmp(c1 int ,c2 int );
insert into tmp values(1,1);
insert into tmp values(2,1);
insert into tmp values(4,1);
insert into tmp values(5,0);
insert into tmp values(6,0);
insert into tmp values(7,0);
insert into tmp values(8,1);
insert into tmp values(9,1);
insert into tmp values(10,1);
insert into tmp values(11,1);
insert into tmp values(12,1);
insert into tmp values(13,0);
insert into tmp values(14,1);
insert into tmp values(15,1);
insert into tmp values(16,1);
insert into tmp values(17,1);
insert into tmp values(18,1);
insert into tmp values(19,1);

--解法1:
select min(c1) start_c1,
max(c1) start_c2
c2
from(select c1,c2,
sum(rn)over(order by c1) rn
from(select c1,
c2,
decode(c2, lag(c2) over(order by c1), null, row_number() over(order by c1)) rn
from tmp
)
)
group by rn, c2;

--解法2:
select min(c1), max(c1), c2
from (select b.*,
row_number()over(partition by g order by c1) r1,
row_number()over(partition by g order by c1 desc) r2
from (select a.*, sum(t) over(order by c1) g
from (select t.*, decode(c2, lag(c2, 1, c2) over(order by c1), 0, 1) t
from tmp t
)a
) b
) c
where r1 = 1 or r2 = 1
group by g, c2
order by 1;

--解法3:
select min(c1) s, max(c1) e, c2
from (select c1, c2, sum(rn) over(order by c1) rn
from (select c1, c2,
case when lag(c2) over(order by c1) = c2 then 0 else 1 end rn
from tmp
)
)
group by c2, rn
order by s
时间: 2024-08-11 10:38:57

根据状态变化情况,求最大值和最小值的相关文章

数组用法----求最大值、最小值和平均数

public class d { /** * @param args */ public static void main(String[] args) { // TODO 自动生成的方法存根 //数组 求最大值.最小值.平均分 int a[]={70,80,90,75,84,88}; int n; int min =100; int max = 1; for(n=0;n<6;n++) { if(max<a[n]) { max=a[n];//循环比较max和a[n]的大小 }else if(m

分组求最大值,最小值 使用开窗函数经验总结

select distinct TT.prod_id, tt.creteTime, tt.inspection_time, tt.cnt from (select s.prod_id, min(s.datetime_created) over(partition by s.prod_id) as creteTime, max(s.inspection_time) over(partition by s.prod_id) as inspection_time, sum(s.complete_cou

POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

使用Python scipy linprog 线性规划求最大值或最小值(使用Python学习数学建模笔记)

函数格式 scipy.optimize.linprog(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None, bounds=None, method='simplex', callback=None, options=None) 今天阅读数据建模第一章线性规划问题,问题描述如下: 通过介绍我们知道了线性规划,就是目标函数及约束条件均为线性函数. 通过画图我们可知,X1,X2的最优解为2,6,目标值为26. 我们如何时候这个scipy的公式来计算这个值呢:

java中数组求最大值,最小值,中和,平均

public class LxJavaCX { public static void main(String[] args) { // 定义一个整型数组,并赋初值 int[] nums = new int[] { 49,61, 23, 4, 74,160, 13, 148, 20 ,150}; int max = nums[0]; // 假定最大值为数组中的第一个元素 int min = nums[0]; // 假定最小值为数组中的第一个元素 double sum = 0;// 累加值 doub

C++数组求最大值及最小值最快方法(3[n/2]的时间效率)

#include <iostream> using namespace std; //最小值和最大值的求解,时间复杂度最多是3[n/2],依据算法导论第九章. void swap(int &a,int &b) { int temp = a; a = b; b = temp; } void Grial(int a[],int n) { int i = 0; if(a[i]>a[i+1]) { swap(a[i],a[i+1]); } for(int j = i+2;j<

zoj 3386 Trick or Treat 三分 求最大值的 最小值

题目来源: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3963 题意:  给定 N 个不同的点, 求在x轴上的 一点,  使 这点到N个点的 距离 最大 的 最小值. f(x) =  max(i){ (xi - x) ^2 + yi ^2 } 求 x  使  min(f(x)) , f(x)为凹函数   ,  采用三分的形式 代码如下: const double EPS = 1e-10 ; const int Max_N

JS数组求最大值和最小值

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

js数组操作 求最大值,最小值,正序、倒叙大小值排序,去重复

var arr = [1,5,2,56,12,34,21,3,5] Math.min.apply({},arr) // 1 Math.max.apply({},arr) // 56 arr.sort((m,n)=>m-n) // [1, 2, 3, 5, 5, 12, 21, 34, 56] arr.sort((m,n)=>n-m) //[56, 34, 21, 12, 5, 5, 3, 2, 1] 去重复 var arr = [2,1,4,3,2,4,2,3,4,2,6,5,5] var o