SQL進階
分組集 GROUP BY
- 分組集 group by field
group by rollup(field),
group by rollup(field1, field2),
group by cube(field1, field2),
group by grouping sets((), field1, field2)select region, avg(production) from t_oil group by region;
select region, avg(production) from t_oil group by rollup(region)
select region, country, avg(production) from t_oil where country in (‘USA’,’Canada’,’Iram’,’Oman’) group by rollup(region, country)
select region, country, avg(production) from t_oil where country in (‘USA’,’Canada’,’Iran’,’Oman’) group by cube(region,country)
select region, country, avg(production) from t_oil where country in (‘USA’,’Canada’,’Iran’,’Oman’) group by grouping sets((),region,country) - 組合分組集 與 filter(where condition)
select region, avg(production) as all, avg(production) filter(where year<1990) as old, avg(production) filter(where year >=1990) as new from t_oil group by rollup(region)
有序集 WITHIN GROUP
中位數
select region, percentile_disc(0.5) within group (order by production) from t_oil group by 1;
select region, percentile_disc(0.5) within group (order by production) from t_oil group by rollup(1);有序集與分組集一起使用
select region, percentile_disc(0.5) within group (order by production) from t_oil group by rollup(1)
HAVING從句
1 | SELECT count(*), town |
循环语句
1 | v_curr_wrkst_no := 'start' |
递归查询
1 | create table digui_test(id int , pid int , name varchar(10)); |
1 | --给出一个数据id号,求出其下所有节点的信息 |
1 | --求出树深度,路径,是否为环数据 |
Explain
select *
from sfc_scan_master_params_d
where request_id in (
select t2.request_id
from del_code_info t1
inner join sfc_scan_master_params t2 on t1.bar_uuid = t2.bar_uuid
where del_lot = ‘5’
);
Gather (cost=9423.40..36679638.18 rows=439194 width=71)
Workers Planned: 2
-> Parallel Hash Semi Join (cost=9423.40..36679638.18 rows=182998 width=71)
Hash Cond: (sfc_scan_master_params_d.request_id = (t2.request_id)::text)
-> Parallel Seq Scan on sfc_scan_master_params_d (cost=0.00..34446288.70 rows=846434370 width=71)
-> Parallel Hash (cost=9300.61..9300.61 rows=9823 width=38)
-> Nested Loop (cost=48.51..9300.61 rows=9823 width=38)
-> Parallel Bitmap Heap Scan on del_code_info t1 (cost=47.95..2054.67 rows=1178 width=8)
Recheck Cond: ((del_lot)::text = ‘5’::text)
-> Bitmap Index Scan on indx_del_code_04 (cost=0.00..47.45 rows=2002 width=0)
Index Cond: ((del_lot)::text = ‘5’::text)
-> Index Scan using idx_sfc_scan_master_params_01 on sfc_scan_master_params t2 (cost=0.57..6.07 rows=8 width=46)
Index Cond: ((bar_uuid)::text = (t1.bar_uuid)::text)
set enable_seqscan = off;
set enable_parallel_hash = off;
Nested Loop (cost=9343.06..363482435.95 rows=439198 width=71)
-> HashAggregate (cost=9342.36..9509.35 rows=16699 width=38)
Group Key: (t2.request_id)::text
-> Gather (cost=48.51..9300.61 rows=16699 width=38)
Workers Planned: 1
-> Nested Loop (cost=48.51..9300.61 rows=9823 width=38)
-> Parallel Bitmap Heap Scan on del_code_info t1 (cost=47.95..2054.67 rows=1178 width=8)
Recheck Cond: ((del_lot)::text = ‘5’::text)
-> Bitmap Index Scan on indx_del_code_04 (cost=0.00..47.45 rows=2002 width=0)
Index Cond: ((del_lot)::text = ‘5’::text)
-> Index Scan using idx_sfc_scan_master_params_01 on sfc_scan_master_params t2 (cost=0.57..6.07 rows=8 width=46)
Index Cond: ((bar_uuid)::text = (t1.bar_uuid)::text)
-> Index Scan using idx_sfc_scan_master_params_d_01 on sfc_scan_master_params_d (cost=0.70..21364.42 rows=40173 width=71)
Index Cond: (request_id = (t2.request_id)::text)
- 執行查詢時,內存調用模型
- 流複製的配置和原理