oracle 唯一值选择查询

gkl3eglg  于 2023-06-22  发布在  Oracle
关注(0)|答案(3)|浏览(150)

我希望获得woo.si_number和stm.serial_number的唯一值。如果我删除“join stock stm on stm.pnm_auto_key = pnm.pnm_auto_key”,我就成功了,但我需要检索si_number。下面是代码:

select woo.si_number, pnm.pn,cmp.company_name, wwt.description, stm.serial_number
from WO_OPERATION woo join parts_master pnm on woo.pnm_auto_key = pnm.pnm_auto_key
                      join companies cmp on cmp.cmp_auto_key = woo.cmp_auto_key
                      join wo_work_type wwt on wwt.wwt_auto_key = woo.wwt_auto_key
                      join stock stm on stm.pnm_auto_key = pnm.pnm_auto_key
where pnm.pn = '5A3307-7' AND
      stm.serial_number like 'BNG%' AND
      wwt.description = 'OVERHAULED'
                 order by woo.si_number

杰夫
编辑:
实际产量:
| si数|PN|公司名称|描述|序列号|
| - -----|- -----|- -----|- -----|- -----|
| 166181| 5A3307-7|西捷航空|大修|BNG0776|
| 166181| 5A3307-7|西捷航空|大修|BNG0776|
| 166181| 5A3307-7|西捷航空|大修|BNG10014|
| 166181| 5A3307-7|西捷航空|大修|BNG10014|
| 166181| 5A3307-7|西捷航空|大修|BNG10014|
| 166181| 5A3307-7|西捷航空|大修|BNG10014|
| 166181| 5A3307-7|西捷航空|大修|BNG10023|
预期输出:
| si数|PN|序列号|
| - -----|- -----|- -----|
| 166181| 5A3307-7| BNG0776|

vulvrdjw

vulvrdjw1#

除非预期输出不完整,否则以下查询应返回预期结果:

select distinct woo.si_number, pnm.pn, stm.serial_number
from WO_OPERATION woo join parts_master pnm on woo.pnm_auto_key = pnm.pnm_auto_key
                      join companies cmp on cmp.cmp_auto_key = woo.cmp_auto_key
                      join wo_work_type wwt on wwt.wwt_auto_key = woo.wwt_auto_key
                      join stock stm on stm.pnm_auto_key = pnm.pnm_auto_key
where pnm.pn = '5A3307-7' AND
      stm.serial_number = 'BNG0776' AND
      wwt.description = 'OVERHAULED'
order by woo.si_number
  • DISTINCT删除重复行。
  • 如果只需要一个特定的serial_number-不要使用通配符。
jtoj6r0c

jtoj6r0c2#

通过添加DISTINCT进行查询,以获取woo.si_number和stm.serial_number的唯一值,同时仍然连接表。通过添加distinct,在执行查询后只返回unique:-

SELECT DISTINCT
  si_number,
  pn,
  company_name,
  description,
  serial_number
FROM
  (SELECT
    woo.si_number,
    pnm.pn,
    cmp.company_name,
    wwt.description,
    stm.serial_number
  FROM
    WO_OPERATION woo
  JOIN
    parts_master pnm ON woo.pnm_auto_key = pnm.pnm_auto_key
  JOIN
    companies cmp ON cmp.cmp_auto_key = woo.cmp_auto_key
  JOIN
    wo_work_type wwt ON wwt.wwt_auto_key = woo.wwt_auto_key
  JOIN
    stock stm ON stm.pnm_auto_key = pnm.pnm_auto_key
  WHERE
    pnm.pn = '5A3307-7' AND
    stm.serial_number LIKE 'BNG%' AND
    wwt.description = 'OVERHAULED') AS subquery
ORDER BY
  si_number;
e37o9pze

e37o9pze3#

每个si编号和pn需要一个结果行。因此,按这两列聚合数据(通过GROUP BY)。不清楚您要根据什么规则来选择每个si编号和pn的序列号。在下面的示例中,我取最小序列号。

select woo.si_number, pnm.pn, min(stm.serial_number) as serial_no
from wo_operation woo 
join parts_master pnm on pnm.pnm_auto_key = woo.pnm_auto_key
join stock stm on stm.pnm_auto_key = pnm.pnm_auto_key
where pnm.pn = '5A3307-7'
and stm.serial_number like 'BNG%' 
and woo.wwt_auto_key = 
(
  select wwt.wwt_auto_key
  from wo_work_type wwt
  where wwt.description = 'OVERHAULED'
)
group by woo.si_number, pnm.pn
order by woo.si_number;

相关问题