gpt4 book ai didi

sql - 如何在oracle 12c中的单个选择中计算每个范围分区的记录?

转载 作者:行者123 更新时间:2023-12-04 14:22:18 25 4
gpt4 key购买 nike

我正在寻找的解决方案(如果存在)是直接按实际分区和 进行分区。不是 按计算值。

表伪代码:

create table table_1 as (part_col DATE, val_col VARCHAR2(1))
partition by range part_col (
PARTITION t_20160515 VALUES LESS THAN (TO_DATE('2016-05-15','YYYY-MM-DD'))
PARTITION t_20160516 VALUES LESS THAN (TO_DATE('2016-05-16','YYYY-MM-DD'))
...
);

选择伪代码:
select partition as P, count(*) as C from table_1
group by partition;

想要的结果
  P        |  C
-----------|-----
2016-05-15 | 8
2016-05-16 | 99

有效但不是我想要的:
select trunc(part_col) P, count(*) C from table_1;

最佳答案

运行它为每个分区生成一组选择

SELECT 'SELECT ' || chr(39) || partition_Name || chr(39) || ', count(*) 
FROM ' ||table_name ||' partition (' || partition_name ||
') UNION ALL ' as test
FROM all_tab_partitions
WHERE table_Name = 'Table_1'

结果如下:
SELECT 'P1', count(*) FROM Table_1 partition (P1) UNION ALL 
SELECT 'P2', count(*) FROM Table_1 partition (P2) UNION ALL
SELECT 'P3', count(*) FROM Table_1 partition (P3) UNION ALL
SELECT 'P4', count(*) FROM Table_1 partition (P4) UNION ALL
SELECT 'P5', count(*) FROM Table_1 partition (P5) UNION ALL

复制并粘贴结果,删除最后一个联合,然后运行。不太确定性能...我打了一个有 5 个分区的表,它在大约 2 秒内完成,总行数只有大约 500 万。

关于sql - 如何在oracle 12c中的单个选择中计算每个范围分区的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38980793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com