oracle 12c-使用width\u bucket和over partition by子句

yjghlzjz  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(491)

我有以下数据集(简化),它包含一个“工作类型”和一个与该工作类型相关联的“任务时间”。

+-----------+----------+--------+
| 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 |
+-----------+----------+--------+

我希望在这个数据集上使用width\u bucket函数。但是,我希望按工作类型对数据进行分区,以便将每个类型分组,而不考虑整个数据集。

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

第一个width_bucket可以工作,但是它将整个数据集的值存储在bucket中。我试着用 OVER (PARITION BY WORK_TYPE) 但是,这会导致以下错误: ORA-00923: FROM keyword not found where expected 有什么想法吗?

lg40wkob

lg40wkob1#

如果希望每组的桶宽度相等,可以为每个group分别计算最小值和最大值:

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

相关问题