简体   繁体   English

如何使用带有 in 运算符的 oracle sql 查询的结果?

[英]How to use results of an oracle sql query with an in operator?

I would like to create a query using an in operator based on the result of a count( ) query having count( ) = 1. Please see query below.我想根据count() = 1 的 count() 查询的结果使用 in 运算符创建查询。请参阅下面的查询。

select count(*), pr.oid
join jrtepiperun pr on pr.oid = it.oid
join xownsparts x on x.oidorigin = pr.oid
having count(*) = 1
group by pr.oid;

This query successfully returns a total of 1589 rows of where count(*) = 1 for each row and pr.oid varies.此查询成功返回总共 1589 行 where count(*) = 1 for each row and pr.oid 变化。 I now want to create another query based on the pr.oid result as shown below.我现在想根据 pr.oid 结果创建另一个查询,如下所示。

select * from jpipelinesystem pl
join xsystemhierarchy x on x.oidorigin = pl.oid
join jrtepiperun pr on pr.oid = x.oiddestination
where pr.oid
in
(
select count(*), pr.oid from
rtrprdb.jrtepiperun pr
join rtrprdb.xownsparts x on x.oidorigin = pr.oid
having count(*) = 1
group by pr.oid
);

However, this returns an error stating that there are too many values.但是,这会返回一个错误,指出值太多。 ORA-00913: too many values 00913. 00000 - "too many values" *Cause: ORA-00913: 太多值 00913. 00000 - “太多值” *原因:
*Action: Error at Line: 20 Column: 1 *操作:行错误:20 列:1

How can I use the results of pr.oid from the first query for the second query?如何将第一个查询的 pr.oid 结果用于第二个查询? Note that I need to have two columns since I want a condition where count(*) = 1.请注意,我需要有两列,因为我想要一个 count(*) = 1 的条件。

Try below way -尝试以下方式 -

you need to remove count(*) from the select list since you are comparing with one value pr.oid您需要从 select 列表中删除count(*) ,因为您正在与一个值 pr.oid 进行比较

Another mistake was the having clause will always be after group by clause另一个错误是having clause总是在group by clause之后

select * from jpipelinesystem pl
join xsystemhierarchy x on x.oidorigin = pl.oid
join jrtepiperun pr on pr.oid = x.oiddestination
where pr.oid
in
(
select pr.oid from
rtrprdb.jrtepiperun pr
join rtrprdb.xownsparts x on x.oidorigin = pr.oid
group by pr.oid having count(*) = 1
);

You can use another approach also.您也可以使用另一种方法。 Use analytical function as follows:使用analytical function如下:

SELECT * FROM 
( SELECT <required columns>, COUNT(1) OVER(PARTITION BY PR.OID) AS CNT
    FROM JPIPELINESYSTEM PL
         JOIN XSYSTEMHIERARCHY X ON X.OIDORIGIN = PL.OID
         JOIN JRTEPIPERUN PR ON PR.OID = X.OIDDESTINATION
         JOIN RTRPRDB.XOWNSPARTS XPR ON XPR.OIDORIGIN = PR.OID
) where CNT = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM