多条记录变一条,字段按位或

select_update_insert 2018-04-12 09:01:12


比如表中这一个date对应四条记录,按位或,组成表二中的一条记录,最终也可根据新组成的type字段,翻译出来原来对应的type的值

简单的说就是多条记录变一条,原来的信息还要保留,要如何实现的?或者有没有更好的办法?交流下
...全文
585 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
卖水果的net 2018-04-12
  • 打赏
  • 举报
回复


SQL> 
SQL> create table test(id, type)
  2  as
  3  select 1,  2 from dual union all
  4  select 2,  8 from dual union all
  5  select 3, 16 from dual union all
  6  select 4, 32 from dual;
Table created
SQL> create or replace function bitor(p varchar2)
  2  return number is
  3    prev number := 0 ;
  4    result number;
  5  begin
  6    for x in (select regexp_substr(p, '[^,]+', 1,level) + 0 value
  7                from dual
  8                connect by rownum  <= regexp_count(p,',') + 1
  9             )
 10    loop
 11      result := (prev + x.value) - bitand(prev, x.value);
 12      prev := result;
 13    end loop;
 14    return result;
 15  end;
 16  /
Function created
SQL> select bitor(listagg(type,',') within group (order by id)) from test;
BITOR(LISTAGG(TYPE,',')WITHING
------------------------------
                            58
SQL> drop function bitor;
Function dropped
SQL> drop table test purge;
Table dropped

SQL> 
  • 打赏
  • 举报
回复
忽略一楼 写错了。。
  • 打赏
  • 举报
回复
直接逗号拼接 type字段不行吗? 可行就是 listagg(type,',') within group(order by 1) from tt group by date
卖水果的net 2018-04-12
  • 打赏
  • 举报
回复
引用 4 楼 wanling_nanjing 的回复:
如果这个date的某一个type失效了,还需要bitand掉
这个逻辑,你自己加一下吧。
  • 打赏
  • 举报
回复
如果这个date的某一个type失效了,还需要bitand掉

17,078

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧