问题描述
我想编写一个存储过程来选择载体,并带有一个附加参数'i_showEmptyCarrier'来指定是否需要隐藏或显示空载体。
+---------------------------+
| Carriers |
+----+----------+-----------+
| id | label | location |
+----+----------+-----------+
| 1 | carrier1 | warehouse |
+----+----------+-----------+
| 2 | carrier2 | warehouse |
+----+----------+-----------+
| 3 | carrier3 | factory |
+----+----------+-----------+
我需要查询“产品”表以检查载体是否为空。
+-------------------------------------------+
| products |
+----+-----------+----------------+---------+
| id | carrierid | productiondate | deleted |
+----+-----------+----------------+---------+
| 1 | 1 | 09/09/2020 | 1 |
+----+-----------+----------------+---------+
| 2 | 1 | 09/09/2020 | 0 |
+----+-----------+----------------+---------+
| 3 | 1 | 09/09/2020 | 0 |
+----+-----------+----------------+---------+
| 4 | 2 | 10/09/2020 | 0 |
+----+-----------+----------------+---------+
| 5 | 2 | 10/09/2020 | 0 |
+----+-----------+----------------+---------+
因此,在这种情况下,carrier3为空。
我想拥有的存储过程逻辑是:
PROCEDURE GetCarriers
(
i_showEmptyCarrier IN number,c_Carriers OUT t_cursor
)
AS
BEGIN
OPEN c_Carriers FOR
SELECT
label,( select count(*)
from products
where products.carrierid = carriers.carrierid
and records.deleted = 0
) as nrOfProducts
FROM carriers
if(i_showEmptyCarrier == 1) {
//select carriers without a product
WHERE nrOfProducts = 0 ;
}
else{
//select carriers with a product
WHERE nrOfProducts > 0 ;
}
END GetCarriers;
因此,如果'i_showEmptyCarrier'= 1,则将选择3号空载波,
+----------+--------------+
| label | nrOfProducts |
+----------+--------------+
| carrier3 | 0 |
+----------+--------------+
否则只会选择1号和2号载波。
+----------+--------------+
| label | nrOfProducts |
+----------+--------------+
| carrier1 | 2 |
+----------+--------------+
| carrier2 | 2 |
+----------+--------------+
解决方法
如何修改查询以使用left join
和conditional aggregation
并最终比较输入,
SELECT label,nrOfProducts
FROM
(
SELECT c.label,SUM(CASE WHEN p.deleted = 0 THEN 1 ELSE 0 END) nrOfProducts
FROM carriers c
LEFT JOIN products p
ON p.carrierid = c.id
GROUP BY c.label
)
WHERE ((&i_showEmptyCarrier = 1 AND nrOfProducts = 0)
OR (&i_showEmptyCarrier = 0 AND nrOfProducts > 0));
您仍然可以在查询中使用where子句,而不是if-else。
,在一种情况下,您需要内部联接,而在另一种情况下,则需要外部联接。由于外部联接只是意味着外部联接行的所有列都为空,因此,在两种情况下都可以简单地进行外部联接,然后才决定是取消还是保留外部联接行。
在SQL中,这很简单:
select *
from carriers c
left join products p on p.carrierid = c.id
where (:show_empty = 'only non-empty' and p.id is not null)
or (:show_empty = 'only empty' and p.id is null)
or (:show_empty = 'all')
(当然,您可以将其汇总到count(p.id)
。在这种情况下,您可以group by c.id
或查看一个运营商where c.id = :carrier_id
。)
似乎您对Java(或其他编程语言)有很大的影响。我不确定您到底想要什么。但是,这就是您实现所需目标的方法。
create or replace procedure p_get_carriers(pi_carrier_flag in varchar2,po_carriers out t_cursor)
is
lt_cursor t_cursor;
begin
select *
bulk collect into lt_cursor
from records
where deleted = case when pi_carrier_flag = 'Y' then 0 else deleted end;
po_carriers := lt_cursor;
exception
when others then
po_carriers := null;
end p_get_carriers;
这意味着您不需要if-else块来处理它。您可以在查询本身的where子句中完成此操作。
注意:我不知道您的表结构和集合详细信息。我猜出来并发布代码。您可能需要相应地进行调整。