oracle的拆分組合查詢

2021-06-03 23:19:43 字數 1963 閱讀 9732

表:msg_content

表:msg_contact_person

想要得到的效果

要點分析:結果表和表1不同的地方是receiver欄位加入了表2的真實姓名

實現方式:

第一步:拆分表1的字段

select c.msg_content_id as contentid,

regexp_substr(receiver, '[^;]+', 1, l) as b

from msg_content c,

(select level l

from dual

connect by level <= 30)

where l <= length(receiver) -

length(replace(receiver, ';')) + 1

得到結果集:

第二步:與表2左連線:

select bb.contentid, bb.b, dd.user_real_name

from (select c.msg_content_id as contentid,

regexp_substr(receiver, '[^;]+', 1, l) as b

from msg_content c,

(select level l

from dual

connect by level <= 30)

where l <= length(receiver) -

length(replace(receiver, ';')) + 1) bb

left join msg_contact_person dd on bb.b = dd.account_name

where bb.b is not null

結果集:

第四步:用wm_concat進行字元的組合,並且加入分頁  ok完成

select *

from (select dd.msg_content_id,

dd.account_name,

cc.receiver,

dd.sender,

dd.realname,

dd.title,

dd.content,

dd.send_time,

dd.if_del,

dd.msg_lv

from msg_content dd,

(select jj.contentid,

replace(wm_concat(jj.b ||

decode(jj.user_real_name,

'','',

'(' || jj.user_real_name || ')')),

',',

';') || ';' receiver

from (select bb.contentid, bb.b, dd.user_real_name

from (select c.msg_content_id as contentid,

regexp_substr(receiver, '[^;]+', 1, l) as b

from msg_content c,

(select level l

from dual

connect by level <= 30)

where l <= length(receiver) -

length(replace(receiver, ';')) + 1) bb

left join msg_contact_person dd on bb.b = dd.account_name

where bb.b is not null) jj

group by jj.contentid) cc

where dd.msg_content_id = cc.contentid

order by dd.msg_content_id desc)

where rownum <= 10

Oracle分組查詢

首先要明白的一點 資料重複的時候分組才有意義。分組查詢語法 select distinct 分組欄位1 別名 分組欄位2 別名 統計函式 from 表名稱 別名 表名稱 別名 where 條件 s group by 分組欄位1 分組欄位2 order by 排序字段 asc desc 排序字段 as...

oracle 分組查詢

組函式 count 個數 sum 求和 g 平均 max 最大值 min 最小值 count 會實際的統計出表中的資料量 count 字段 如果統計的字段上不包含有 null,那麼與 count 結果相同 如果統計欄位上包含有了 null,null 不參與統計 count distinct 字段 消...

拆分,組合謎題 分割手鍊問題 分割砝碼問題

1 分割秤砣問題 乙個 40g的砝碼被分成 4塊,這 4塊恰好可以稱出 1 40g 之間所有重量 整數 求這 4塊分別多重.答案 1,3,9,27 這正好組成了三進製數的基,正好有三種狀態 每個砝碼 放左盤,放右盤,不放。所以用它們組合能表示出 1 40 的所有整數。2 切割手鍊問題 引自 啊哈,靈...