过滤到不重叠的范围 - Amazon RedShift

时间:2017-04-19 14:55:50

标签: sql amazon-redshift

背景:我的范围经常更新,以便为不同数量的材料设置价格。一旦达到某些配额,价格就会下降。问题是在更新或添加范围后确定当前价格。

我希望从数据集中过滤出非连续范围。这是一些测试代码:

<?php
$data = '
{
    "name":"Custom filter less than 1000",
    "type":"deals",
    "visible_to":1,
    "conditions":{
        "glue": "and",
        "conditions":[
            {
                "glue": "and",
                "conditions": [
                        {
                            "object": "deal",
                            "field_id": "12452",
                            "operator": "<",
                            "value": 1000,
                            "extra_value": null
                        }
                    ]
                },
            {
                "glue": "or",
                "conditions": []
            }
        ]
    }
}
';

$ch = curl_init("https://api.pipedrive.com/v1/filters?api_token=xxx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json;', 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST,           1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);

echo $response;

此代码将返回:

drop table if exists public.test_ranges;
create table public.test_ranges (
    category        integer
    ,lower_bound    integer
    ,upper_bound    integer
    ,cost           numeric(10,2)
    ,modifieddate   timestamp
);

insert into public.test_ranges values (1,0,70456,0,'2015-09-29');
insert into public.test_ranges values (1,53956,60000,1.28,'2015-02-11');
insert into public.test_ranges values (1,70456,90000,1.02,'2015-09-29');
insert into public.test_ranges values (1,90000,120000,0.88,'2015-02-11');
insert into public.test_ranges values (1,120000,999999999,0.79,'2015-02-11');

insert into public.test_ranges values (2,0,48786,0,'2015-11-02');
insert into public.test_ranges values (2,22500,25000,0.43,'2015-02-17');
insert into public.test_ranges values (2,48786,50000,0.37,'2015-11-02');
insert into public.test_ranges values (2,50000,100000,0.21,'2015-02-17');
insert into public.test_ranges values (2,100000,175000,0.19,'2015-02-17');
insert into public.test_ranges values (2,175000,999999999,0.17,'2015-02-17');

insert into public.test_ranges values (3,0,585969,0,'2015-11-02');
insert into public.test_ranges values (3,346667,375000,0.15,'2014-09-12');
insert into public.test_ranges values (3,375000,500000,0.14,'2014-09-12');
insert into public.test_ranges values (3,500000,600000,0.13,'2014-09-12');
insert into public.test_ranges values (3,585969,999999999,0.02,'2015-11-02');
insert into public.test_ranges values (3,600000,670000,0.12,'2014-09-12');

select * from  public.test_ranges order by 1,2;

期望的结果:

category  lower_bound  upper_bound  cost  modifieddate
--------------------------------------------------
1         0            70456        0     2015-09-29 
1         53956        60000        1.28  2015-02-11
1         70456        90000        1.02  2015-09-29 
1         90000        120000       0.88  2015-02-11
1         120000       999999999    0.79  2015-02-11 
2         0            48786        0     2015-11-02
2         22500        25000        0.43  2015-02-17 
2         48786        50000        0.37  2015-11-02
2         50000        100000       0.21  2015-02-17 
2         100000       175000       0.19  2015-02-17
2         175000       999999999    0.17  2015-02-17
3         0            585969       0.00  2015-11-02
3         346667       375000       0.15  2014-09-12
3         375000       500000       0.14  2014-09-12
3         500000       600000       0.13  2014-09-12
3         585969       999999999    0.02  2015-11-02
3         600000       670000       0.12  2014-09-12

提前感谢您的帮助。

0 个答案:

没有答案