te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>window functions - Oracle Max Over Partition By Excluding Current Row - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

window functions - Oracle Max Over Partition By Excluding Current Row - Stack Overflow

programmeradmin3浏览0评论

I have an issue to calculate the max() value over partition by where i want to exclude the current row each time. Assuming I have a table with ID, Group and Valbue. calculating max/min/etc. over partition by Group is strghait forward. however, If I want to calculate the group's MAX() for example excluding the ID's value. meaning, assuming I have 2 groups, with 3 IDs each. the MAX() for the first ID in the the first group would be between the other 2 IDs of that group (#2 and #3), the MAX() for the second ID for that group would be, again between the other two IDs (#1 and #3), and for the last ID of that group we'll have the max between value #1 and #2 (each row's value should be excluded from the aggregation of it's group)

Please assist

I have an issue to calculate the max() value over partition by where i want to exclude the current row each time. Assuming I have a table with ID, Group and Valbue. calculating max/min/etc. over partition by Group is strghait forward. however, If I want to calculate the group's MAX() for example excluding the ID's value. meaning, assuming I have 2 groups, with 3 IDs each. the MAX() for the first ID in the the first group would be between the other 2 IDs of that group (#2 and #3), the MAX() for the second ID for that group would be, again between the other two IDs (#1 and #3), and for the last ID of that group we'll have the max between value #1 and #2 (each row's value should be excluded from the aggregation of it's group)

Please assist

Share Improve this question edited Feb 18 at 0:12 MT0 168k12 gold badges66 silver badges127 bronze badges asked Feb 17 at 23:21 RabersRabers 451 silver badge9 bronze badges 1
  • Why did you remove the example data from the question. It's harder to understand now. – Barmar Commented Feb 17 at 23:41
Add a comment  | 

3 Answers 3

Reset to default 3

You can use the windowing clause of the analytic function to exclude the current row:

SELECT id,
       grp,
       value,
       COALESCE(
         MAX(value) OVER (
           PARTITION BY grp
           ORDER BY value
           ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING
         ),
         MAX(value) OVER (
           PARTITION BY grp
           ORDER BY value
           ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
         )
       ) AS max
FROM   table_name

Which, for the sample data:

CREATE TABLE table_name (id, grp, value) AS
SELECT 1, 1, 1 FROM DUAL UNION ALL
SELECT 2, 1, 2 FROM DUAL UNION ALL
SELECT 3, 1, 3 FROM DUAL UNION ALL
SELECT 4, 2, 4 FROM DUAL UNION ALL
SELECT 5, 2, 5 FROM DUAL UNION ALL
SELECT 6, 2, 5 FROM DUAL;

Outputs:

ID GRP VALUE MAX
1 1 1 3
2 1 2 3
3 1 3 2
4 2 4 5
5 2 5 5
6 2 5 5

fiddle

From Oracle Database 21c the window frame has an exclude clause you can use to omit the current row from the calculation:

create table table_name (id, grp, value) as
  select 1, 1, 1 from dual union all
  select 2, 1, 2 from dual union all
  select 3, 1, 3 from dual union all
  select 4, 2, 4 from dual union all
  select 5, 2, 5 from dual union all
  select 6, 2, 5 from dual;

select t.*,
  max ( value ) over (
    partition by grp 
    order by id
      rows between unbounded preceding and unbounded following
      exclude current row
  ) max_except_current
from table_name t;

        ID        GRP      VALUE MAX_EXCEPT_CURRENT
---------- ---------- ---------- ------------------
         1          1          1                  3
         2          1          2                  3
         3          1          3                  2
         4          2          4                  5
         5          2          5                  5
         6          2          5                  5

Other options for this are:

  • exclude no others (default) include the whole window
  • exclude group omit rows with the same value for the sort columns
  • exclude ties omit other rows with the same value for the sort columns; include the current row

Use a self-JOIN that excludes the same row.

SELECT t1.id, t1.group, t1.value, MAX(t2.value) AS Max_Excluded
FROM Your_Table AS t1
JOIN Your_Table AS t2 ON t1.group = t2.group AND t1.id <> t2.id
GROUP BY t1.id, t1.group, t1.value
发布评论

评论列表(0)

  1. 暂无评论