简体   繁体   English

Oracle 12C - 使用 Width_Bucket 并通过子句进行过度分区

[英]Oracle 12C - Using Width_Bucket with Over Partition By Clause

I have the following dataset (simplified) that constists of a 'WORK_TYPE' and a 'TASKTIME' associated with that work type.我有以下数据集(简化),由与该工作类型关联的“WORK_TYPE”和“TASKTIME”组成。

+-----------+----------+--------+
| WORK_TYPE | TASKTIME | OUTPUT |
+-----------+----------+--------+
| TYPE1     |       10 |      1 |
| TYPE1     |       20 |      1 |
| TYPE1     |       30 |      2 |
| TYPE1     |       30 |      2 |
| TYPE2     |       10 |      1 |
| TYPE2     |       10 |      1 |
| TYPE2     |       20 |      2 |
| TYPE2     |       20 |      2 |
+-----------+----------+--------+

I wish to use the width_bucket function on this dataset.我希望在这个数据集上使用 width_bucket function。 However I want to partition the data by the work_types so each type is grouped up irrespective of the entire dataset.但是,我想按 work_types 对数据进行分区,因此每种类型都被分组,而与整个数据集无关。

SELECT 
      TASKTIME
     ,WORK_TYPE
     ,WIDTH_BUCKET(TASKTIME,0,100,30) AS TASKTIME_BUCKET
     ,WIDTH_BUCKET(TASKTIME,0,100,30) OVER (PARTITION BY WORK_TYPE) AS TASKTME_BUCKET_WT --This Errors
FROM TABLE1

The first width_bucket works, however buckets the values across the whole dataset.第一个 width_bucket 有效,但是将值存储在整个数据集中。 I tried to use the OVER (PARITION BY WORK_TYPE) after the width_bucket, however this is causing the following error: ORA-00923: FROM keyword not found where expected我尝试在 width_bucket 之后使用OVER (PARITION BY WORK_TYPE) ,但这会导致以下错误: ORA-00923: FROM keyword not found where expected

Any ideas?有任何想法吗?

If you want equal width buckets for each group, you can calculate separate min and max values for each gruop:如果您想要每个组的宽度相等的桶,您可以为每个组计算单独的最小值和最大值:

SELECT TASKTIME, WORK_TYPE,
       WIDTH_BUCKET(TASKTIME, 0, 100, 30) AS TASKTIME_BUCKET
       WIDTH_BUCKET(TASKTIME, MIN_TASKTIME, MAX_TASKTIME, 30) AS TASKTME_BUCKET_WT 
FROM (SELECT t1.*,
             MIN(TASKTIME) OVER (PARTITION BY WORK_TYPE) as MIN_TASKTIME,
             MAX(TASKTIME) OVER (PARTITION BY WORK_TYPE) as MAX_TASKTIME
      FROM TABLE1 t1
     ) t1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM