在相关子查询

时间:2017-11-28 14:37:49

标签: amazon-redshift

Count(*)返回两个不同的结果,即使条件没有显着变化。 这里的查询应该在count(*)列中返回相同的值,而不管注释/未注释的部分:

select count(*) as regular_count, some_date, sum((call_count>0)::int) as active_count
  --  , count(distinct random_corresponding_varchar) -- uncomment this part accordingly
FROM (
       SELECT
         es.random_corresponding_varchar,
         DATE(some_date) AS some_date,
         (SELECT count(*)
          FROM inner_sample cs
          WHERE cs.random_varchar = es.random_corresponding_varchar) AS call_count
       FROM outer_sample es
       GROUP BY es.random_corresponding_varchar, DATE(some_date)
     ) as a
group by some_date
order by some_date

示例场景(只是盲目地运行它,数据在这里大部分都无关紧要):

CREATE TABLE inner_sample
(
  id VARCHAR(256),
  random_varchar VARCHAR(256)
);
CREATE TABLE outer_sample
(
  random_corresponding_varchar VARCHAR(384),
  some_date DATE
);
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('a4d099a37563aef9825f6ae70b3e7c56e0ad81aa', 'jdp5htrtlx');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('8a6d2dff4e439c60bed044562db7af6e9e30af5e', 'wcsna7t3zg');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('4681f4a47d043def97d4d95aaf189491d51e8503', 'wcsna7t3zg');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('35932f4dd18260abf098b17c4abc2acd2134de62', 'vcb0udhl49');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('31db53f5806318cb4a4c2938c0dbff35f08fb7d9', 'jdp5htrtlx');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('0996af69d67e5397dae2f37771c2bc031251c99a', 'wcsna7t3zg');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('1b866177db992492dd7a11f24060c2f5ef110f99', 'qurrkyspzb');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('4fe969fa1c558626e9bd39c8a01e5f5cc60ebc03', 'jdp5htrtlx');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('8995f0f27737acddcb0c5999a383fa4500682daa', 'wcsna7t3zg');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('2e0e5a7f49924fa3c949e87c23c17eda9ab82711', 'vcb0udhl49');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('de3b7c7a829973bfdfaf91fe87ed3658466cb1d9', 'jdp5htrtlx');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('3f9cce1e9fa789a906ce235f7a566369830a322b', 'wcsna7t3zg');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('19f41e2e69601ce965673311b1598084186fbee1', 'wcsna7t3zg');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('d05c6320801ef167cbe454bb422bceab809080a0', 'jdp5htrtlx');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('70e051422c1851c6ac2aa8d7cd6d9271cb6dcb1a', 'vcb0udhl49');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('1d62b71119263da45990dd48f934977f1b84be91', 'jdp5htrtlx');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('98aca15bde5006cc70d6f84dbf323ec0e9bb45a2', 'wcsna7t3zg');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('508e369d792bfcc314be35b3fffbed3a9561a100', 'jdp5htrtlx');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('9bb73d890ad773ed60d6d510e6a5a412dd55c49f', 'wcsna7t3zg');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('17a8cd7387831813eaa0117fdb8e6c4d2e3091fc', 'rziyhrwoj2');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('0d9eee14d164507dbb797088b50cc16bdbba377e', 'wcsna7t3zg');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('b620deb2afdd11195e578b1696d5bb488957b87f', 'wcsna7t3zg');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('017d7e19d4ed21a2dbf74d92cb1202448168c0c0', 'jdp5htrtlx');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('6ec79f8c688026a7ff40135e8254b53c24962abc', 'vcb0udhl49');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('51c15f3306cbb4018f56495b7cc92d93f74fa79e', 'jdp5htrtlx');
INSERT INTO public.inner_sample (id, random_varchar) VALUES ('b80b9c87c09538f48ec55c78537089f99231213a', 'jdp5htrtlx');
INSERT INTO public.outer_sample (random_corresponding_varchar, some_date) VALUES ('n3ptpo8upy', '2017-08-01');
INSERT INTO public.outer_sample (random_corresponding_varchar, some_date) VALUES ('i5mqqg5xlm', '2017-08-01');
INSERT INTO public.outer_sample (random_corresponding_varchar, some_date) VALUES ('mxpr0vazlj', '2017-08-01');
INSERT INTO public.outer_sample (random_corresponding_varchar, some_date) VALUES ('uenymh253f', '2017-08-01');
INSERT INTO public.outer_sample (random_corresponding_varchar, some_date) VALUES ('cwufxsms1x', '2017-08-01');
INSERT INTO public.outer_sample (random_corresponding_varchar, some_date) VALUES ('yacnezje0u', '2017-08-01');
INSERT INTO public.outer_sample (random_corresponding_varchar, some_date) VALUES ('jdp5htrtlx', '2017-08-01');
INSERT INTO public.outer_sample (random_corresponding_varchar, some_date) VALUES ('riq1jav7xt', '2017-08-01');
INSERT INTO public.outer_sample (random_corresponding_varchar, some_date) VALUES ('vcb0udhl49', '2017-08-01');
INSERT INTO public.outer_sample (random_corresponding_varchar, some_date) VALUES ('qurrkyspzb', '2017-08-01');
INSERT INTO public.outer_sample (random_corresponding_varchar, some_date) VALUES ('rziyhrwoj2', '2017-08-01');
INSERT INTO public.outer_sample (random_corresponding_varchar, some_date) VALUES ('dno31jc9ke', '2017-08-01');
INSERT INTO public.outer_sample (random_corresponding_varchar, some_date) VALUES ('iqvggayhwg', '2017-08-01');
INSERT INTO public.outer_sample (random_corresponding_varchar, some_date) VALUES ('rbfdfqsdrw', '2017-08-01');
INSERT INTO public.outer_sample (random_corresponding_varchar, some_date) VALUES ('zhf29sxwki', '2017-08-01');
INSERT INTO public.outer_sample (random_corresponding_varchar, some_date) VALUES ('wcsna7t3zg', '2017-08-01');
INSERT INTO public.outer_sample (random_corresponding_varchar, some_date) VALUES ('qysvkjf3my', '2017-08-01');

1 个答案:

答案 0 :(得分:0)

这可能是RedShift不支持的相关子查询的示例: https://docs.aws.amazon.com/redshift/latest/dg/r_correlated_subqueries.html

  

这种子查询在其列与外部查询产生的列之间包含一个或多个相关性...

     

查询计划程序使用称为子查询去相关的查询重写方法来优化几种相关子查询模式,以便在MPP环境中执行。几种类型的相关子查询遵循Amazon Redshift无法解相关且不支持的模式...