作者jimbosanho (chacha)
看板Database
标题Re: [SQL ] oracle 同一栏位重复只列出一笔
时间Thu Jan 27 05:36:20 2011
※ 引述《jimbosanho (chacha)》之铭言:
: 小弟最近开始接触SQL
: 是在公司使用oracle 8i的资料库
: 想问如果有比父子资料是像以下这样
: 父 子
: ________ _________
: A 1
: A 2
: B 1
: B 2
: B 3
: 但是我要印出的资料只要有一笔父的资料
: 父 子
: ________ _________
: A 1
: 2
: B 1
: 2
: 3
: 重复的值选择不列印
: 但是父子是在同一个栏位
: 意及
: 人
: ____________
: A
: 1
: 2
: B
: 1
: 2
: 3
: 第一次发问,请多包含
终於解出来,放上来让大家看看吧!
select segment1,
description
from(
select msib.segment1,
msib.description,
msib.inventory_item_id
from bom_bill_of_materials bbom,
mtl_system_items_b msib
where bbom.assembly_item_id = msib.inventory_item_id
and exists ( select 1
from bom_inventory_components bic
where bic.bill_sequence_id = bbom.bill_sequence_id)
union all
select ' ' ||msib.segment1,
msib.description,
msib.inventory_item_id
from bom_inventory_component bic,
mtl_system_items_b msib
where bic.component_item_id = msib.inventory_item_id
and exist (select 1
from bom_bill_of_materials bbom
where bic.bill_sequence_id = bbom.bill_sequence_id)
)
order by inventory_item_id,segment1 desc
注解: union以上的部分为串出有Component(子)的Assembly(父),以下相反
用union all的原因是,不同的Assembly可能会有同一个component,只是
bill_sequence_id不一样,为了不要让重复的components被化成一笔
另外最後先以inventory_item_id排序,让父子排序在一起,再作segment1
的排序是要让父亲排在儿子之前
後记:另外应该可以用connect by 跟start with来作,可是我不会,也希望能有人提供
使用方法,学SQL的第三个礼拜,路还很漫长...
bbom = bom表,储存assembly的资讯
msib = item的项目资讯
bic = component的资讯
msib --< bbom --< bic --< msib
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 210.242.142.10